System.UnauthorizedAccessException from closing my app?

Posted on 2011-10-20
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

    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

    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)
    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

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now