command line - Problem with spaces in a filepath - commandline execution in C# -
i building gui commandline program. in txtboxurls[textbox] file paths entered line line. if file path contains spaces program not working properly. program 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 process finish proc.waitforexit();
for example if file path entered in txtboxurls "c:\vs2008\projects\web2pdf\web2pdf\bin\release\test page.htm", program not work. file path double quotes work in windows command line(i not using gui) nicely. solution.
proc.startinfo.arguments = text + " " + txtboxurls.text + " " + txtfilename.text;
in line, text
contains quoted version of txtboxurls strings. why add them again in unquoted form (+ txtboxurls.text
)? if understood code corrently, following should work:
proc.startinfo.arguments = text + " " + txtfilename.text;
in fact, since txtfilename.text
contain spaces, should quote well, sure:
proc.startinfo.arguments = text + " \"" + txtfilename.text + "\"";
(or, using syntax:)
proc.startinfo.arguments = text + @" """ + txtfilename.text + @"""";
Comments
Post a Comment