0

The following code will not write to the registry that i have in there but will with many others i have tried. It runs the code and kicks back nothing. It appears to have worked till i check the reg and nothing is there.

I have the proper permissions, but i can't figure out why this key is so special. I can manually go in and create it but i need this program to do it. Thanks in advance.

string machineName = txtBox.Text;
RegistryKey subKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName).OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", true);
subKey.SetValue("DeltmpfileOnReboot", @"cmd.exe /c RD /S /Q C:\data\tempFolder /f", RegistryValueKind.String);

Found the key... it magically appeared at HKLM\SOFTWARE\Wow6432Node. Odd...

EDIT Found the issue... Write the registry value without redirect in Wow6432Node second answer by hans passant... it always something silly like that

Community
  • 1
  • 1
Garrett
  • 155
  • 1
  • 2
  • 8
  • 2
    Does the account that is running this code have the permissions to write to that key? – Oded Nov 16 '12 at 14:14
  • What version of windows are you running ? do you need to have elevated permissions ? (Vista/7/8) – Micah Armantrout Nov 16 '12 at 14:14
  • Sorry forgot to add, Windows 7 and yes i am running as admin and it does have permissions. – Garrett Nov 16 '12 at 14:15
  • Is the code running under a domain account that has proper permissions on the remote machine? – prprcupofcoffee Nov 16 '12 at 14:16
  • Does the target machine have "Remote Registry" enabled? – Lloyd Nov 16 '12 at 14:20
  • Yes it does, i have written other keys to it seconds before i run this one... same exact way – Garrett Nov 16 '12 at 14:21
  • 1
    You probably have the UAC slider set to the "don't bug me!" setting. So you don't see that Regedit.exe silently elevates itself to get sufficient rights. This won't work for your program, you have to add the manifest to ask for elevation. – Hans Passant Nov 16 '12 at 14:36

1 Answers1

1

Your approach is wrong. If you want to delete a file on reboot you'd be better off using the Win32 API MoveFileEx.

See here:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240(v=vs.85).aspx

And:

http://www.pinvoke.net/default.aspx/kernel32.MoveFileEx

You simply set the target to null and specify MOVEFILE_DELAY_UNTIL_REBOOT in flags.

Lloyd
  • 29,197
  • 4
  • 84
  • 98