Im trying to access the registry to change a file association and I keep getting a null error. The variable FullPath is null. Code: try { Microsoft.Win32.RegistryKey ClassesRoot = Microsoft.Win32.Registry.ClassesRoot; Microsoft.Win32.RegistryKey FullPath = ClassesRoot.OpenSubKey(".ulz", true); FullPath.SetValue("", "wud.CompressedUL"); } catch (SystemException ex) { MessageBox.Show(ex.Message); } I know this is probably WOW64 redirection but i don't know how to fix it. I am coding this on a Win7 x64 computer and I have tried running this on a x32 computer and I get the same null reference exception. Also i have been trying to figure out how to create a file in the system32 folder in Win7 x64 but it puts it in the SysWOW64 folder instead. If anyone can help that would be awesome thanks!
Well, if have an null return, the registry not has been readed, this code will solve the problem, reading the registry, and if not exist, create the sub key, patching the permissions for an secure mod: Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Win32; using System.Security.AccessControl; using System.Security.Principal; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { RegistryKey RegKey = null; { try { Registry.ClassesRoot.CreateSubKey(".ulz", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryOptions.Volatile); RegKey = Registry.ClassesRoot.OpenSubKey(".ulz", true); } catch { MessageBox.Show("Critical error on registry read"); } } try { RegistryAccessRule Rules = new RegistryAccessRule(WindowsIdentity.GetCurrent().User, RegistryRights.FullControl, AccessControlType.Allow); { RegistrySecurity Security = new RegistrySecurity(); Security.SetOwner(WindowsIdentity.GetCurrent().User); Security.AddAccessRule(Rules); RegKey.SetAccessControl(Security); } RegKey.SetValue("", "wud.CompressedUL"); } catch { MessageBox.Show("Fails on registry write"); } } } } PS: Aparently, the WOW64 keys is present only in HKLM
Well, the kernel will recognize the Sysnative as System32 dir in x64 systems... This code will help you with native System32 file write with x86 apps: Code: if (Directory.Exists(Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative")) { File.WriteAllBytes(Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative", Properties.Resources.MyRes); } else { File.WriteAllBytes(Environment.GetEnvironmentVariable("WINDIR") + "\\System32", Properties.Resources.MyRes); }
Thanks again everyone both things work now. I used this code for the second one. Code: public static string SystemDirectory { get { if (Directory.Exists(Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative")) { return (Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative"); } else { return (Environment.GetEnvironmentVariable("WINDIR") + "\\System32"); } } } Alphawaves sorry for some reason I couldn't get yours to work it still redirected to the other folder. Thanks though.
Just checked again, its NOT working on my x64 system only x86.. I compiled using AnyCPU, that why it worked before.
It's strange, the Environment.GetFolderPath(Environment.SpecialFolder.System) gets the System32 folder... Anyway, I've been tested this code: Code: public static string GetSystemDirectory() { if (!Environment.GetFolderPath(Environment.SpecialFolder.SystemX86).ToUpper().Contains("SYSTEM32")) return Environment.GetEnvironmentVariable("WINDIR") + "\\Sysnative"; return Environment.GetFolderPath(Environment.SpecialFolder.SystemX86); } Modded the Environment.SpecialFolder.SystemX86 instead of System...