这似乎很简单,但我似乎无法找出问题所在
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/