SOF User
SOF User

Reputation: 7830

Access Denied in creating directory using C#

string windowsDirectory = Environment.GetEnvironmentVariable("ProgramFiles");
string mydirecoty = windowsDirectory + "\\" + "NetServices\\";

if (!Directory.Exists(mydirecoty))
  {
    Directory.CreateDirectory(mydirecoty); //Access to the path 'C:\Program Files (x86)\NetServices\' is denied.
  }

exception is thrown when I run my .net application without Administrator account. While If I try to create directory in other then C:\ drive like D:\ it runs fine.

What is solution that I want to create directory in Program Files/ Windows folder?

Upvotes: 2

Views: 7730

Answers (5)

Carmelo La Monica
Carmelo La Monica

Reputation: 765

if you use operating systems higher than Windows XP, you will not able to access C: \ PrpgramFiles protecded because reading and writing, unless AVII your application with Administrator rights.

Bye

Upvotes: 0

Ahmed
Ahmed

Reputation: 7238

You need to elevate user privileges as it is a feature in windows vista/7. Running as admin will solve the problem.

Upvotes: 0

Ahmed Magdy
Ahmed Magdy

Reputation: 6030

if you are running this from web application make sure that the application pool user has ntfs permission of that folder.

Upvotes: 0

Ed Manet
Ed Manet

Reputation: 3178

If your application is intended to be run by the user, write to the user's app data folder or temp folder. Your app should install to the Program Files directory, and perhaps use some files in the Windows directory, but it should never have to write to either location. You can easily get the user's app daa folder from the environment variables.

Upvotes: 0

marc_s
marc_s

Reputation: 754478

The C:\program files folder is protected (by design) in Vista and Windows 7 (and Windows Server 2008 / 2008 R2) - normal user accounts do not have any permission to create directories in there - it's a system folder.

Either you need to run as admin - then you have permission to create directories even in protected system folders - or you create the directories elsewhere and not inside a protected system folder. The second option would be the recommended and preferred option.

Upvotes: 6

Related Questions