“对路径...的访问被拒绝” (.NET C#)

发布于 2024-08-05 17:26:49 字数 1448 浏览 2 评论 0原文

我一直在将一个小的 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));
            }
        }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桜花祭 2024-08-12 17:26:50

在另一个答案的评论中,您说这是一个桌面应用程序,所以让我们分别处理每个位置。

在Vista及更高版本下,普通用户无权在系统驱动器根目录(通常是C:)中创建文件。您可以通过在资源管理器中打开 C:\,右键单击并尝试创建文件来亲自查看这一点 - 您应该会收到 UAC 提示。因此,如果您想写入 C:\,那么您的应用程序需要以管理员身份运行,通过适当的清单要求提升,或者在您想要写入该位置时启动单独的进程。

但是,应用程序数据、Environment.SpecialFolder.ApplicationData 应该可以工作。如果输出返回的实际目录,您会得到什么?

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?

错々过的事 2024-08-12 17:26:50

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

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.

始终不够爱げ你 2024-08-12 17:26:50

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

除此之外,IIS 用户没有权限访问 Adam 已经提到的其他文件夹

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文