以下代码片段在Windows Vista或Windows 7上运行良好,但在XP上运行不正常:
String filename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ".html");
[...write file...]
System.Diagnostics.Process.Start("excel.exe", "\"" + filename + "\"");
Run Code Online (Sandbox Code Playgroud)
问题是,在Windows XP上filename包含空格("c:\ documents and settings ..."),因此在XP Excel上只显示错误"无法打开c:\ documents.xls".
在Windows Vista和7上,当我将路径/文件名设置为包含空格的内容时,它甚至可以工作.
有没有办法更改参数,以便它也将在Windows XP上打开,或者我是否必须更改所有客户端计算机上的临时目录?
尝试使用:
Process excelProcess = new Process();
excelProcess.StartInfo.FileName = "excel.exe";
excelProcess.StartInfo.Aguments = "\"" + filename + "\"";
excelProcess.Start();
Run Code Online (Sandbox Code Playgroud)
我知道这段代码适用于文件名中的空格.