Nathan
Nathan

Reputation: 1347

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 :)

Upvotes: 5

Views: 14887

Answers (2)

edtheprogrammerguy
edtheprogrammerguy

Reputation: 6049

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> 

Upvotes: 4

David Pilkington
David Pilkington

Reputation: 13618

I think that the App needs to run as Administrator.

Try and add this to the manifest

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

Upvotes: 1

Related Questions