0

I'm trying to open an xml file created on the fly. Here is the code where I write the file to disk, then read it immedately.

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

Code that does the writing:

// 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);
    }

Error information:

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.

What causes this Exception? The filename is exactly the same, yet I can't read the document. The file is created (I can navigate to it in Windows Explorer). Does Process.Start() search in another location?

Paul Seeb
  • 6,006
  • 3
  • 26
  • 38
Matt
  • 437
  • 3
  • 10
  • If you double-click the file in Windows Explorer, does it open? – James Curran Aug 06 '14 at 14:43
  • Yes it does. 200ish lines of xml. – Matt Aug 06 '14 at 14:44
  • http://stackoverflow.com/a/2369171/2654498 – Nick Aug 06 '14 at 14:44
  • try fully qualifying the filepath instead of doing it in relation to your current working directory. System.Diagnostics.Process.Start may be starting in a different location. – vesuvious Aug 06 '14 at 14:46
  • @Matt, but what is it opening in? – James Curran Aug 06 '14 at 14:47
  • You don't have a real problem, a "first chance exception" is meaningless for XML serialization. As you can tell, you actually got the .xml file out of your code. The underlying reason is explained in the linked duplicate. – Hans Passant Aug 06 '14 at 14:47
  • @HansPassant - But his problem isn't with the serialiation, but with the Process.Start that follows it. He's getting a completely different error. – James Curran Aug 06 '14 at 14:53
  • @JamesCurran - You're correct. The problem isn't with XML Serialization. The exception is thrown on Process.Start(), saying the file cannot be found. The file is opening in my default browser, Internet Explorer. – Matt Aug 06 '14 at 14:55

1 Answers1

1

My only suggestion is that you are using Linux forward slashes, instead of the Windows backward-slashes. It should be:

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

although I'd prefer:

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

I'm gonna guess that StreamWriter is a bit more forgiving than Process.Start about them being wrong.

Matt
  • 437
  • 3
  • 10
James Curran
  • 101,701
  • 37
  • 181
  • 258