-1

I'd like my console application to update files in a specified directory. However, I am getting Permission Denied message when attempting to use File.WriteAllLines method.

Is there a way to run my console application to allow this update. I'd rather not have to manually give directories permission to Everyone or something like that (Or what might work is to adjust permission from c# then reset when I'm done, maybe)

Rod
  • 14,529
  • 31
  • 118
  • 230
  • The reason your application is getting access denied is because the program does not have rights. Can you switch to a different folder that you do have rights to write to (Using `SpecialFolder.CommonApplicationData` instead of `SpecialFolder.ProgramFiles` for example to hold your config data for example) – Scott Chamberlain Apr 24 '15 at 16:34

2 Answers2

2

Instead of giving permission and taking back to the current user, Run your application by impersonating a user with admin (required privileges) credentials.

See: How do you do Impersonation in .NET?

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
1

If you're looking for a method to require your console application to be executed with administrative privileges then the easiest method is to add an application manifest file to your project and in that, add the following tag:

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

This is not as flexible as impersonation because you're limiting the application to be only run by administrators, with full permissions but I thought I'd point it out as a simpler alternative in case it might fit your needs.

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151