c# - XmlTextWriter - 访问路径被拒绝

标签 c# xml

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

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

我可以知道我错在哪里吗?

最佳答案

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

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

也许你想要的东西是:

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

关于c# - XmlTextWriter - 访问路径被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21913893/

相关文章:

c# - 如何为有限的代码部分使用命名空间

c# - 打开一个表单,关闭它,然后在同一位置再次打开它

c# - 如何以编程方式隐藏桌面图标?

xml - 在 Windows Phone 7 上使用 XDocument 时从抛出 NullReferenceException 的 XML 文档加载数据

c# - 单声道 : Set a program to launch at startup for alternative OS

c# - 从字符串创建 IronPython(动态)对象

java - Android Studio - 在 java 代码中使用 XML id

c# - 比较两个 XML 文件并显示它们之间的差异并更新文件

PHP SimpleXML + 获取属性

c# - 如何使 C# Web 服务生成 soapenv 命名空间而不是 soap?