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?
The
C:\program filesfolder 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.