Javad Yousefi
Javad Yousefi

Reputation: 2300

Need to run as administrator in C# App

I write a C# application that needs to write in a file that is in the program files folder in Windows Drive. and for this, I must run my app as administrator otherwise an error happened. I want to run my app in normal form ( not as administrator ). Is there a solution?

Upvotes: 2

Views: 3794

Answers (2)

Scott Chamberlain
Scott Chamberlain

Reputation: 127593

You have a few options (in order of preferred option)

  1. Don't write to Program Files! You should not be modifying files after the program is installed. If you need files that are writable and common to all users you should be writing to the folder returned by Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
  2. Run the program as an administrator always by adding a manifest file (see Ehsan's answer)
  3. Change the folder permissions on the folder you are trying to access so members of the Users group can write to the folder instead of only being able to read it.

Upvotes: 3

Ehsan
Ehsan

Reputation: 32691

Add an application manifest file from project-->Add File-->Manifest and change this line as

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

Upvotes: 2

Related Questions