Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
2.14/5 (3 votes)
See more:
Hi,

I am trying to create folder in C:\\Program Files\\ using code like this:
Directory.CreateDirectory("C:\\Program Files\\folderName").

But showing exception as : Access to the path 'C:\Program Files\folderName' is denied.

Please help me out.

Thanks,
Posted
Updated 25-Apr-13 3:10am
v2
Comments
[no name] 25-Apr-13 8:45am    
You need to run your application as an admin.
CHill60 25-Apr-13 8:46am    
Do you have permissions on that PC to create folders?
Member 7686563 25-Apr-13 8:51am    
Ya, if run exe as Administrator it is running if not its showing exception.
But I don't want to select everytime manually.
Is any possiblities to set permission for creating folder/files by c# code?
Thanks,

1 solution

Two solutions to the permissions problem:

You need to set the appropriate permissions to Read from the source and Write to the target location for the user(s) of the exe.

OR

You can impersonate a user with such permissions (in the code, use the credentials of another user to perform the action).
** See A complete Impersonation Demo in C#.NET[^]

Cheers,
Edo
 
Share this answer
 
v2
Comments
Member 7686563 25-Apr-13 9:20am    
Hi Edo,
unable to download source code.please check out.
Thanks,
Joezer BH 25-Apr-13 9:45am    
The source code of the referenced article?

* If so, try this other article: http://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User

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