12

我一直在将一个小的 XML 数据文件保存到外部驱动器,没有问题。但后来我尝试使用 ApplicationData 文件夹和其他文件夹,甚至 C:\ 但没有运气。我收到类似“访问路径“C:\”被拒绝”的错误。

只是为了确认,该文件已创建并使用当前代码正常读取到外部驱动器。我想这与安全和权限有关,但我没有发现任何有用的东西。

如果您能在此问题上指出正确的方向,请提前致谢!

        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));
            }
        }
4

3 回答 3

11

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?

于 2009-09-20T14:02:18.897 回答
2

我只能想象应用程序必须在无权访问本地驱动器的用户的上下文中运行,例如在匿名 IIS 帐户下运行的 ASP.NET 网站或只能访问相关网络的服务帐户地点。

于 2009-09-20T12:55:06.280 回答
1

外部驱动器很可能是用 FAT 格式化的。FAT 不支持对用户进行权限管理,所以保存在那里是可以的。

除此之外,IIS 用户对已经提到的 Adam 等其他文件夹没有任何权限

于 2009-09-20T12:57:11.743 回答