开发者

Process.Start parameter containing spaces has problems on XP?

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 W开发者_开发知识库indows 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?


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.


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.


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"
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜