Problem with spaces in file path - command line execution in C #

I am creating gui for a command line program. In txtBoxUrls [TextBox], the file path is entered line by line. If the file path contains spaces , the program does not work properly. The program is given below.

string[] urls = txtBoxUrls.Text.ToString().Split(new char[] { '\n', '\r' });

string s1;
string text;
foreach (string s in urls)
{
    if (s.Contains(" "))
    {
        s1 = @"""" + s + @"""";
        text += s1 + " ";
    }
    else
    {
        text += s + " ";
    }
}


System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.CreateNoWindow = true;


proc.StartInfo.FileName = @"wk.exe";


proc.StartInfo.Arguments = text + " " + txtFileName.Text;

proc.StartInfo.UseShellExecute = false;


proc.StartInfo.RedirectStandardOutput = true;


proc.Start();

//Get program output
string strOutput = proc.StandardOutput.ReadToEnd();

//Wait for process to finish
proc.WaitForExit();

For example, if the file path is specified in txtBoxUrls, it is "C: \ VS2008 \ Projects \ web2pdf \ web2pdf \ bin \ Release \ Test Page.htm", the program will not work. This path to the file with double quotes will work on the Windows command line (I do not use the graphical interface). What will be the solution.

+3
source share
3 answers
proc.StartInfo.Arguments = text + " " + txtBoxUrls.Text + " " + txtFileName.Text; 

text txtBoxUrls. (+ txtBoxUrls.Text)? , :

proc.StartInfo.Arguments = text + " " + txtFileName.Text;    

, txtFileName.Text , , :

proc.StartInfo.Arguments = text + " \"" + txtFileName.Text + "\"";    

(, :)

proc.StartInfo.Arguments = text + @" """ + txtFileName.Text + @"""";    
+8

, , . , , . - ...

wk.exe "C:\VS2008\Projects\web2pdf\web2pdf\bin\Release\Test Page.htm"

, . , :

s1 = @"""" + s + @"""";
+2

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


All Articles