суббота, 28 августа 2010 г.

How to open file with spaces in command line using C#



I wanted to open files in their default application using C#. My first idea was to run cmd and pass there a file name as parameter.

System.Diagnostics.Process proc = new System.Diagnostics.Process();
pproc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c \"start filename.docx\"";
proc.StartInfo.CreateNoWindow = true;
proc.Start();

The problem is that I can't use file names with spaces.


The solution is:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "my file name.docx";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();

Комментариев нет: