System.UnauthorizedAccessException from closing my app?

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)
dkim18Asked:
Who is Participating?
 
strickddCommented:
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.
0
 
p_davisCommented:
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.
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
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:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

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"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.