System.UnauthorizedAccessException from closing my app?

Posted on 2011-10-20
Medium Priority
Last Modified: 2013-12-16
I used the Setup project to create an installer and it installed OK.
But I try to close my app, it is giving me this error.

My app creates this xml file in my application folder under program Files folder.

If I use the ClickOnce install, it works fine and closes.

Why not use ClickOnce install then?

My VS is not creating the Setup.exe file but only .application file (today).
It created the setup file last night but today and i have same project.
It is driving me nut!

Any sugget as why I am getting this error?

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\WebTool\Settings.xml' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
Question by:dkim18
LVL 22

Assisted Solution

p_davis earned 400 total points
ID: 37006669
Is that file in use by any other process at that point? That would deny you access.

you could use sysinternals Process Explorer to find out what process, if any is accessing that file when you get the error. May not be the solution but it is a place to start.
LVL 28

Accepted Solution

strickdd earned 800 total points
ID: 37006672
ClickOnce applications run in a "sandbox" under you app data folder. This allows for your user to have full control over all the files in the project when it is running and therefore can read/write/modify Settings.xml.

When you do an install and try to write to a protected directory on a Windows Vista/7 machine, you will get an access denied. You have two options:

1) Run the application as an administrator. (Right-click option)
2) Store the settings file in the user's application data file by using a user settings file.
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 800 total points
ID: 37009572
Under .NET, C:\Program Files is read-only if the user is a non-administrator. This is usually not the right place to store application files that need to be written to.

The right place is in the application data directory of the user, which you can get with the following code:


You can create a directory under that one and access it. It is recommended to have a directory with the name of the company, and under that one a directory for each application, something like:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\YourCompany\YourApplication"

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question