Skip to content

Instantly share code, notes, and snippets.

@tfl
Created October 21, 2010 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfl/638576 to your computer and use it in GitHub Desktop.
Save tfl/638576 to your computer and use it in GitHub Desktop.
Compile and run it as scheduled task - it fails here! It does not fail when run logged on.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.IO;
using System.Security.Principal;
namespace regtest
{
class Program
{
static void Main(string[] args)
{
StreamWriter sw = new StreamWriter("regtest.log");
try
{
RegistryKey HKLM = Registry.LocalMachine;
try
{
WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
string isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator) ? "Yes" : "No";
sw.WriteLine("Is the user admin: " + isAdmin);
// Apply Your path here!!
RegistryKey General = HKLM.OpenSubKey("your\\path\\here", false);
try
{
// Apply Your setting!!
sw.WriteLine("OK: " + General.GetValue("@YOUR KEY@")");
}
catch (Exception c)
{
sw.WriteLine("Could not read key: " + c.ToString());
}
}
catch (Exception b)
{
sw.WriteLine("Could not open path: " + b.ToString());
}
}
catch (Exception a)
{
sw.WriteLine("Could not open HKLM: " + a.ToString());
}
sw.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment