views:

4803

answers:

4

Hi!

I've been saving a small XML data file to an external drive, no probs. But then I tried to use the ApplicationData folder and others, even C:\ but no luck. I'm getting an error like "Access to the path "C:\" denied".

Just to confirm, the file is created and read fine with the current code, to an external drive. I guess this is something to do with security & permissions but I haven't found anything too useful.

Thanks in advance if you can point me in the right direction on this one!

        string fipData = @"F:\IL2\SIIYM\SIIYM Data.xml";  //  external drive ok :-)
        //string fipData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        //string fipData = @"C:\";

        //  if the XML data file doesn't exist, create it
        bool dataFileExists = File.Exists(fipData);
        if (dataFileExists)
        {
            //  read the XML values
            XDocument xData = XDocument.Load(fipData);
            //...
        }
        else
        {
            //  create & save the XML data file
            XElement xLastPath = new XElement(el_lastPath, "");
            XElement xLastCode = new XElement(el_lastCode, "");

            XElement xRoot = new XElement(el_root);
            xRoot.Add(xLastPath);
            xRoot.Add(xLastCode);

            XDocument newDataFile = new XDocument();
            newDataFile.Add(xRoot);

            try
            {
                newDataFile.Save(fipData);
            }
            catch (Exception ex)
            {   
                MessageBox.Show("Data file unable to be created. System message:{0}".Put(Environment.NewLine + Environment.NewLine + ex.Message));
            }
        }
+1  A: 

I can only imagine that the application must be running in the context of a user which does not have access to the local drive, e.g. an ASP.NET website running under the anonymous IIS account or a service account which only has access to the relevant network locations.

AdamRalph
Oops, the context here is: * My personal PC * Windows 7 * Desktop appIs it just that my UAC settings (default) are too high perhaps? How can I know if it will work on another person's computer?
Gregg Cleland
+1  A: 

Most likely the external drive is formated with FAT. FAT does not support rights management for users, so saving there is ok.

Besides that the IIS User has no rights to the other folders like Adam mentioned already

Heiko Hatzfeld
+1  A: 

In the comments to another answer you say this is a desktop application, so lets treat each location separately.

Under Vista and beyond, an ordinary user does not have rights to create files in the root directory of the system drive (usually C:). You can see this for yourself by opening C:\ in explorer, right clicking and trying to create a file - you should get a UAC prompt. So if you want to write to C:\ then your application needs to run as an administrator, via a suitable manifest demanding elevation, or by starting a separate process when you want to write to that location.

Application Data, Environment.SpecialFolder.ApplicationData should however work. If you output the actual directory that returns what do you get?

blowdart
Thanks for the reply!Environment.SpecialFolder.ApplicationData:C:\Users\MrGreggles\AppData\Roaming\SIIYM Data.xml
Gregg Cleland
It works now, beats me why.
Gregg Cleland
AppData should always work, that's very strange!
blowdart
A: 

This solution worked for me.