Link to home
Start Free TrialLog in
Avatar of rmmarsh
rmmarshFlag for United States of America

asked on

Access to path is denied

I have the following line of code; when running on Vista or Win 7, I get the above message.  So I figured that I'm not supposed to put application data in the Program Files directory.

So where is it supposed to go?
string xmlFilename = System.AppDomain.CurrentDomain.BaseDirectory + "Options.xml";

            if (File.Exists(xmlFilename))
            {
                File.SetAttributes(xmlFilename, FileAttributes.Normal);  //  so we can write on the file
                using (XmlReader tr = XmlReader.Create(xmlFilename))
                {

Open in new window

Avatar of Praveen Venu
Praveen Venu
Flag of India image

Right click on the folder where Options.xml exist
Select properties
Select Security Tab
type ASPNet and click OK
Now select ASPNET and set the read and write permissions
Avatar of rmmarsh

ASKER

Can't right click on the folder because it's on the user's machine.

Also, this is not ASPNET, it's C# .NET...
Avatar of rmmarsh

ASKER

Actually, I changed the path to take it out of the c:\program files directory.  Now, I don't get an error on the read, but only on the write.  New code is below.

            string filename = @"C:\Prager\prager.Options.xml";

            XmlTextWriter tw = null;
            try
            {
                tw = new XmlTextWriter(filename, null);  //null represents the Encoding Type
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("prager.Options.xml' is denied"))
                {
                    MessageBox.Show("You do not have write permission to the prager.Options.xml file.  Disable UAC or run this program as Administrator, otherwise your options will not be saved.", "Prager, Software", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Carnou
Carnou
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kenneniah
Kenneniah

What Carnou said. You need to use the users profile to store program data, as Vista and Windows 7 by default don't allow programs to write to other locations. This has long been a programming rule and guideline from Microsoft for security. In Windows 7 and Vista they are finally enforcing it.
Avatar of rmmarsh

ASKER

Thank you so much... I appreciate it.