1
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?

love Computer science
  • 1,824
  • 4
  • 20
  • 39
SOF User
  • 7,590
  • 22
  • 75
  • 121
  • 1
    Is this a web based application, windows service, executable or scheduled task? – Kane Jul 03 '11 at 12:03
  • Please edit your question with the details of the exception, in your code you should at least have a try/catch around the code. Is it running on Windows 7? Are you 100% sure you are an admin and UAC is disabled? In general you should not create a subfolder in that location unless you are installing an application but then that is usually done by an installer. – Davide Piras Jul 03 '11 at 12:04
  • You can force your application to run as administrator: http://stackoverflow.com/questions/2818179/how-to-force-c-app-to-run-as-administrator-on-windows-7 – Shadow The GPT Wizard Jul 03 '11 at 12:07
  • @lovecomputerscience - it's not dictionary it's Directory. – Shadow The GPT Wizard Jul 03 '11 at 12:08
  • 1
    I suggest you avoid to create the folder in there at all :) – Davide Piras Jul 03 '11 at 12:10

5 Answers5

6

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

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

Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50
Ahmed
  • 7,148
  • 12
  • 57
  • 96
0

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.

Ed Manet
  • 3,118
  • 3
  • 20
  • 23
0

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

Ahmed Magdy
  • 5,956
  • 8
  • 43
  • 75
0

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

Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50