Question: Using Windows 7, Unauthorized Access Exception when running my application
Asked Answered
F

2

5

My application is raising an unauthorized access error. While running my application, I try to access a directory in the following location: Application.UserAppDataPath.

The Problem: It says I do not have permission to access the Application.UserAppDataPath directory

Is there a way to set permissions within my application source code?

Something like:

Application.UserAppDataPath.SetPermissions()
Foreignism answered 4/3, 2010 at 1:58 Comment(3)
Please show us your code; you're probably making a mistake.Trio
If you don't have permission to access the path, it stands to reason that you won't have permission to set permissions on the path... but you should always have permissions to the user app data.Audiogenic
The code that throws the exception is this: StreamReader sr = new StreamReader(Application.UserAppDataPath));Foreignism
A
13

Looking at your comment, you say this is your code:

StreamReader sr = new StreamReader(Application.UserAppDataPath);

Application.UserAppDataPath is a directory, not a file. If you try to open that directly, it's the same as trying to open a file one level below the AppData folder, which you really don't have permission to do.

Use Path.Combine to construct a path to a file inside the AppData folder, i.e.

string fileName = Path.Combine(Application.UserAppDataPath, "settings.xml");
StreamReader sr = new StreamReader(fileName);

Of course this is just an example - in reality you should probably be using a sub-folder inside AppData specific to your application.

Audiogenic answered 4/3, 2010 at 2:38 Comment(1)
This is most likely the cause, and one that I've seen too often. You definately want to create a subdirectory underneath AppData that is specific to your application and use that.Godavari
D
2

Its probably a UAC issue, Try running your application as an elevated process, and see if the error persists

Daile answered 4/3, 2010 at 2:2 Comment(3)
Thank you. How do I run it as an elevated process? Sorry, im a n00b.Foreignism
@RedEye: You can right-click on your program's shortcut and choose 'Run as Administrator'.Thorazine
@sniperX - +1 - ju got it maing.Mazur

© 2022 - 2024 — McMap. All rights reserved.