I have a project which references another project in order to utilise some portions of code. One of the projects is a console application (that is the project type) but is made up of a significant code-base. The other is a GUI application that references this console application . The reason this is done is so the application can be run either as a console application or a GUI application.
When the GUI application runs it accesses the registry absolutely fine, but when I run the console version, which accesses the exact same code as the GUI application , it fails security-wise to get access to the registry keys. I'm assuming both the GUI and console application would run via the same user.
Here's what's failing (changes made to hide specific details):
String user = Environment.UserDomainName + "\\" + Environment.UserName;
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user, RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.WriteKey | RegistryRights.FullControl, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow));
mapsynckey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\AppPath\\AppName\\");
UserName = mapsynckey.GetValue("user").ToString();
Password = mapsynckey.GetValue("pass").ToString();
SyncFolderPath = mapsynckey.GetValue("syncfolderpath").ToString();
Ignore the access rights, I've just been trying every single one hence the random mixture. The error I receive is as follows:
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\AppPath\AppName' is denied
Here's the other thing. I can't find the entries anywhere in the registry, not even under the 64-bit section. However, I am loading the registry key details into the application which are shown fine, but if I browse the registry, they're not there.
I'm completely stumped, I've spent the last 3 hours working on this with no clue what's going on.
at ApplicationSettings..ctor() in C:\Projects\MapSync\MapSyncConsole\ApplicationSettings开发者_开发知识库.cs:line 34
at ApplicationSettings.get_Instance() in C:\Projects\MapSync\MapSyncConsole\ApplicationSettings.cs:line 47
at MainApplication..ctor(Boolean forceDownload) in C:\Projects\MapSync\MapSyncConsole\MainApplication.cs:line 27
at MapSyncConsole.Program.Main(String[] args) in C:\Projects\MapSync\MapSyncConsole\Program.cs:line 21
First and foremost: Did you check the (UAC) virtualized registry locations? (See here). Registry uses a similar mechanism as file system for transparent redirection.
Additionally:
- which token is your process running under, elevated or not? Assuming you have UAC enabled: Do you start the console app from a elevated prompt (seems not)? I guess you start the UI version by double-clicking?
- less probable: does one of the executables have an embedded manifest which manipulates the elevation behavior?
- less probable: do the processes (console vs. UI) target different processor architectures?
You can't call CreateSubKey
in HKLM without administrator privileges.
You should consider switching to HKCU.
精彩评论