System.Unauthorizedaccessexception While Running .Exe Under Program Files

Unauthorized Access Exception writing file from .exe

I think that it is a problem with permissions to the folder.
Probably Visual Studio runs your application as an administrator and the .EXE file is executed as a normal user.

why to I get this error: System.UnauthorizedAccessException: 'C:\Windows\system32\CSV_file'

This happens because when your program is called by the batch the current folder is not the one you have your executable and windows doesn't care to change the current directory to your folder. The only thing that matters is the availability of your program and this is granted because you give the full path to the START command.

You can fix your problem changing the START command and specifying the folder where your program should be run

START /D C:\Debug C:\Debug\DiagnisticTool.exe

or adding a CD command just before running the file

CD C:\DEBUG
START C:\DEBUG\DiagnosticTool.exe

or changing the path passed to StreamWriter

using (StreamWriter outputFile = File.AppendText(@"C:\debug\PerformanceLogFile.csv"))

this last option, while allowing to leave your batch as is, could be a problem if you want to customize the output folder. For example if you are not allowed to create a C:\DEBUG folder on the target machine or for some other reason.

Access to path denied while reading a dll from Program Files which is actually got from a nuget package

For this application, even for reading the DLLs from the Program Files folder I needed Admin rights so I forced the application to have such rights for execution.

The below line for the newly created application manifest file is changed and that solved the issue.

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

The fix is explained well in How do I force my .NET application to run as administrator?

The reasons are stated well in https://stackoverflow.com/a/50588465/129130

System.UnauthorizedAccessException (A first chance exception)

You need to run your app as admin.

Here's he right way to do it:

http://blogs.msdn.com/b/nikhiln/archive/2007/04/19/embed-a-manifest-to-make-an-application-elevate-in-vista.aspx



Related Topics



Leave a reply



Submit