tags:

views:

156

answers:

3

The following code snippet runs nicely on windows vista or windows 7, but not on XP:

String filename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ".html");
[...write file...]
System.Diagnostics.Process.Start("excel.exe", "\"" + filename + "\"");

Problem is, on Windows XP the filename contains spaces ("c:\documents and settings..."), so on XP Excel just shows the error "can't open c:\documents.xls".

On Windows Vista and 7 it even works when I set the path/filename to something containing spaces.

Is there a way to change the parameters so it will open on windows XP, too, or will I have to change the temp directory on all my clients computers?

+2  A: 

If excel.exe is the default executable for the xls file extension on your system, you can just use:

Process.Start(filename)

Alternatively you could try to find the short 8.3 style path name and use that as the argument.

Peter van der Heijden
*cough* if you'd have read my example, you would have noticed that my file extension is *not* xls, but html. But I still want excel to open the file, not the browser, so I can't use your version of the call.
Sam
Any clues how to get the 8.3 filename from the path/name I got from GetTempFileName?
Sam
Sorry, missed that. Actually I did read your question and example, just did not bother to scroll.
Peter van der Heijden
All the examples I can find use P/Invoke e.g. http://www.c-sharpcorner.com/UploadFile/crajesh1981/RajeshPage103142006044841AM/RajeshPage1.aspx
Peter van der Heijden
+1  A: 

Try using:

Process excelProcess = new Process();

excelProcess.StartInfo.FileName = "excel.exe";
excelProcess.StartInfo.Aguments = "\"" + filename + "\"";

excelProcess.Start();

I know this code works with spaces in the filename.

ChrisF
Does your code work on XP with spaces?On Vista/7 my version with the FileName and Arguments on the constructor does work, too.
Sam
@Sam - yes - I did double check it just to be sure though :)
ChrisF
+1  A: 

I don't have Excel on my WinXP x64 SP2 but notepad worked even without quotes:

string filename = Path.ChangeExtension(Path.GetTempFileName(), ".html");
File.AppendAllText(filename, "Test");

System.Diagnostics.Process.Start("notepad.exe", filename);

But if it is not working for you I think it is worth trying to set working directory and provide only the file name:

string filename = .Path.ChangeExtension(Path.GetTempFileName(), ".html");
File.AppendAllText(filename, "Test");

System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
    Arguments = Path.GetFileName(filename),
    WorkingDirectory = Path.GetDirectoryName(filename),
    FileName = "excel.exe"
});
Regent
I just copied your first example (changing `notepad.exe` back to `excel.exe`) and ran it. Works fine on 32 bit WinXP too.
MikeJ-UK
And your temp directory does contain spaces? Or was it changed to some path without spaces?
Sam
These are the files I get, as expected :-C:\Documents and Settings\myname\Local Settings\Temp\tmp15.tmp (0 bytes)C:\Documents and Settings\myname\Local Settings\Temp\tmp15.html (4 bytes)
MikeJ-UK
@Sam: It does contain spaces. I just checked and it was `C:\Documents and Settings\Regent\Local Settings\Temp\tmpCF2.html`
Regent
@Sam: But have you tried setting `WorkingDirectory` as in example in this answer?
Regent
@Regent, I have not found time yet, but it looks very promising to me, so I definitely will!
Sam
Setting the workingdirectory did the trick, thanks for the idea!!
Sam