简体   繁体   中英

Create Directory - Access denied

When I try to create a new directory in Program Files for my program I'm thrown an error saying access is denied. This is the error: Additional information: Access to the path 'C:\\Program Files\\PointOfSales' is denied. Why's this? I couldn't find any C# specific help that solved my problem. This is the code I'm using now.

if (!Directory.Exists("C:\\Program Files\\PointOfSales"))
        {
            Directory.CreateDirectory("C:\\Program Files\\PointOfSales");
        }

Thanks :)

Your program running as the current user does not have the permissions to create a directory in the Program Files location. Some locations require admin privileges (for either the program or the user) to create Directories and other file I/O operations.

If the program won't be allowed to run as admin, have the installer program create the directories for you. Many user applications won't be allowed to run as admin (as part of company security policies), so creating directories in protected locations won't be possible in those situations.

If you let the installer do it, you can specify requireAdministrator (/level='requireAdministrator') in a C++ installer program for UAC Execution Level option.

For a C# program you can put the following in your manifest to insure it runs as admin:

<security>
  <requestedPrivileges>
    <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
  </requestedPrivileges>
</security> 

I think that the App needs to run as Administrator.

Try and add this to the manifest

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM