Click here to Skip to main content
15,889,693 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friends,

I face a problem in accessing the registry,
and creating folder in "c:\windows".

Exception:
System.Security.SecurityException: Requested registry acess is not allowed.

System.UnauthorizedAccessException: Access to the path 'c:\windows\MH\' is denied.

Code:
RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp", true);
string portNumber = key.GetValue("PortNumber").ToString();string pathWin = Environment.GetFolderPath(System.Environment.SpecialFolder.Windows);
Directory.CreateDirectory(@pathWin + @"\MHTest");

The program worka on Windows 2008, but it didn't work in windows 7.
I run the program with Administrator user.

Thanks
Posted
Updated 24-Dec-10 4:44am
v2
Comments
JF2015 24-Dec-10 10:44am    
Added code formatting.

Sounds like the Win7 extra security layer. Even if you run through the administrator account, if the system security is at high level, it prompts for permission to avoid access to files/folders programmatically/automatically.

As you are already sure of the admin priviledges to the account used, just loosen the security of Win7. It has 4 levels as much I remeber. Allow the security to pass on wihtout prompts and see. It should work then. For now, its definitly the priviledge issue and nothing related to code.
 
Share this answer
 
Comments
Moustafa Safwat 24-Dec-10 11:06am    
You are right, it seems to be the logic for those.
But could you please tell me how to modify system security.
anther hint; I compared actually the permission between the both users on W2008 and W7, but they are same permission "Special Permission".
Thank you for helping :)
Sandeep Mewara 24-Dec-10 11:21am    
Sure. Try:
Control Panel -> Action Center -> Change User Account control setting -> (Pick the one that suits you. You can try 2/3/4 level)
Moustafa Safwat 24-Dec-10 11:30am    
you are good man :), I set it to level 2 th same exception happened, but the advanced this tis time when I tried to run the program "as Administrator" it worked.
Sandeep Mewara 24-Dec-10 11:34am    
Good to know it helped! :)

I ran the same piece of code on Windows 7 and did not get any error. It executed fine and also created the folder. I agree with Sandeep, since my User Account Control is set to UAC never notify
 
Share this answer
 
Comments
Moustafa Safwat 24-Dec-10 11:12am    
sounds great, I tried from a while also to run it on virtual machine "Windows XP" and it worked, but I tested it on other virtual machine "Windows 7" with anther version but not worked.
Thank you for helping :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900