开发者

Access Denied in creating directory using C#

开发者 https://www.devze.com 2023-03-16 19:49 出处:网络
string windowsDirectory = Environment.GetEnvironmentVariable(\"ProgramFiles\"); string mydirecoty = windowsDirectory + \"\\\\\" + \"NetServices\\\\\";
string windowsDirectory = Environment.GetEnvironmentVariable("ProgramFiles");
string mydirecoty = windowsDirectory + "\\" + "NetServices\\";

if (!Directory.开发者_如何学PythonExists(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?


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.


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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号