为什么我收到这个Win32Exception?

时间:2014-08-06 14:38:51

标签: c# xml file exception filesystems

我正在尝试打开动态创建的xml文件。这是我将文件写入磁盘的代码,然后立即读取它。

string filename = "/Files/Runs/" + LoginServer.CurrentUsername + "/NEMSIS/" + SaveDestination + ".xml";
y.WriteToFile(filename);
System.Diagnostics.Process.Start(filename);

写作的代码:

// Write an EMSDataset to file
public void WriteToFile(string filename)
    {
        var Ser = new XmlSerializer(typeof(EMSDataSet));
        using (StreamWriter Sw = new StreamWriter(filename))
            Ser.Serialize(Sw, _nemsisDocument);
    }

错误信息:

A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

Additional information: The system cannot find the file specified

If there is a handler for this exception, the program may be safely continued.

导致此异常的原因是什么?文件名完全相同,但我无法阅读文档。该文件已创建(我可以在Windows资源管理器中导航到该文件)。 Process.Start()在其他位置搜索吗?

1 个答案:

答案 0 :(得分:1)

我唯一的建议是你使用Linux正斜杠,而不是Windows反斜杠。它应该是:

 string filename = @"\Files\Runs\" + LoginServer.CurrentUsername +
                   @"\NEMSIS\" + SaveDestination + ".xml";

虽然我更喜欢:

 string filename = String.Format(@"\Files\Runs\{0}\NEMSIS\{1}.xml", 
                     LoginServer.CurrentUsername, SaveDestination ;

我猜错StreamWriterProcess.Start对他们错了更宽容。