Garrett
Garrett

Reputation: 155

Can't Write to registry

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

Upvotes: 0

Views: 1012

Answers (1)

Lloyd
Lloyd

Reputation: 29668

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.

Upvotes: 1

Related Questions