I have a Winfom app created in VS 2012, just has a button which calls below code. If I run the app in Admin mode(Run As Admin) it is able to create the folder. But in non-admin mode(double click on the exe in explorer), it always pops up standard "Access to the path 'C:\Program Files (x86)\DirTest' is denied" error.
My doubt is, why its not write in Win 7 Virtual Store when running the app in non-admin mode, whats is missing?
`string programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string appDir = Path.Combine(programFiles, "DirTest");
if (Directory.Exists(appDir))
MessageBox.Show(appDir + " does exists!!!");
else
{
MessageBox.Show(appDir + " does NOT exists!!!");
Directory.CreateDirectory(appDir);
}`
The "Program Files" directory is protected by UAC in Vista-onwards. Some discussion of this can be found at: Why can't I edit a “Program Files” file on Windows 7?
So its working as designed, you can't muck with Program Files as an external program unless run as administrator. If you intend to always be run as administrator, add the following line to your app manifest:
That way you can double-click on the application, it will prompt for UAC access, and be run as Administrator.