【问题标题】:XmlTextWriter - Access to the path is deniedXmlTextWriter - 访问路径被拒绝
【发布时间】:2014-02-20 16:32:01
【问题描述】:

这似乎很简单,但我似乎无法找出问题所在

public static string destinationFile;

[STAThread]
private static void Main(string[] args)
{
    //doing something and then calling convert method
}

private static void convert(object source, FileSystemEventArgs f)
{
    if (check(FileName))
    {
        //doing something

        XmlTextWriter myWriter = new XmlTextWriter(destinationFile, null);

        //doing something
    }
}

private static bool check(string filename)
{
    //check the file and return a boolean result
    if (sometest)
    {
        destinationFile = @"d:/GS";
        return true;
    }

    return false;
}

当我运行它时,我得到:

The process failed:
System.UnauthorizedAccessException: Access to the path is denied

请问我哪里出错了。

【问题讨论】:

  • Administrator 运行您的程序并重试
  • 我用的是windows xp,已经是管理员帐号了
  • 您确定管理员有权访问该文件夹吗?
  • 检查你是否有完全的访问权限——读/写目标文件
  • 是的,我之前有一个疑问,然后我将每个人都放在了该文件夹中,并具有完全访问权限

标签: c# xml


【解决方案1】:

您正在尝试写入一个文件,该文件实际上已经是您文件系统上的一个文件夹。

在您的check 方法中,您将destinationFile 设置为“D:\GS”,然后您使用destinationFile 作为XmlTextWriter 的目标。

可能你想要的东西是:

  XmlTextWriter myWriter = new XmlTextWriter(Path.Combine(destinationFile, FileName), null);

【讨论】:

  • 这并不完全有效,但从这个观点的启发下,我发现了我的问题。我使用的变量错误。我使用的是完整路径而不是名称,这会产生问题。但是感谢您的解决方案。
【解决方案2】:

尽管异常消息具有误导性,但这可能表明文件已隐藏。

您可以通过右键单击 Windows 资源管理器 => 属性来检查。 “隐藏”复选框是否已激活?如果是,请取消选中并重试。

您也可以在应用程序的代码中删除它(如果业务逻辑允许这样做)。你可以在这里找到示例代码:How do I write to a hidden file?

【讨论】:

  • 我正在尝试创建 xml 文件。使用 XMLTEXTwriter 引发异常
【解决方案3】:

要开始调试,我可能会在同一位置放置一些非常简单的 xml

var xml = "<?xml version=\"1.0\"?><hello><world>hello world</world></hello>";
 XDocument xdoc = XDocument.Parse(xml);
 xdoc.Save("d:\test.xml");

然后尝试在新路径中读取 看看你的xml阅读器是否可以访问测试文件

【讨论】:

    猜你喜欢
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多