Does the Process.Start parameter containing spaces have problems with XP?

The following code snippet works fine 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 + "\"");

The problem in Windows XP filenamecontains spaces ("c: \ documents and settings ..."), so XP Excel simply shows the error "cannot open c: \ documents.xls".

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

Is there a way to change the settings so that it also opens in Windows XP, or will I need to change the temp directory on all the computers of my clients?

+3
source share
3 answers

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

, , :

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

:

Process excelProcess = new Process();

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

excelProcess.Start();

, .

+8

If it excel.exeis the default executable for the file extension xlson your system, you can simply use:

Process.Start(filename)

Alternatively, you can try to find the short path name in 8.3 style and use this as an argument.

+2
source

Source: https://habr.com/ru/post/1755746/


All Articles