【问题标题】:Why am I receiving this Win32Exception?为什么我会收到此 Win32Exception?
【发布时间】:2014-08-06 14:38:51
【问题描述】:

我正在尝试打开一个动态创建的 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() 是否在其他位置搜索?

【问题讨论】:

  • 如果您在 Windows 资源管理器中双击该文件,它会打开吗?
  • 是的。 200 多行 xml。
  • 尝试完全限定文件路径,而不是相对于您当前的工作目录进行。 System.Diagnostics.Process.Start 可能在不同的位置启动。
  • @Matt,但它打开的是什么?

标签: c# xml file exception filesystems


【解决方案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 更宽容他们的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2020-02-24
    • 2013-12-09
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多