Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
hi guys i have a C# application that executes a batch file

the problem i have is on the application i have a multi line textbox
and the text if this textbox i need to pass as a parameter to my batch file ?

in this the ?????? is what i need ? or some other way how to do it .
the comment is the text from the multi line text box
tagProcess.StartInfo.FileName = @"FILENAME";
//new line hack from C# to Batch
comment = comment.Replace("\r\n",????????);
tagProcess.StartInfo.Arguments = string.Format("{0} {1}",  tag , "\"" + comment + "\"");

tagProcess.StartInfo.UseShellExecute = false;
tagProcess.Start();
tagProcess.WaitForExit();




any help will be nice thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 30-Aug-11 15:30pm    
Why on Earth "??????"? Why multiline text box? Is a line a parameter?
--SA

1 solution

First, your "\r\n" is non-portable; right end-of-line is: System.Environment.NewLine.

Now, I have no idea what do you mean by "????????". It must be just a blank space, " ". Also, it's safer to parse the text in separate line and get each line in quotation marks. This is for the case when you mean each line to be a separate argument. The thing is: there can be blank spaces inside an argument.

To do it, use string.Split:
C#
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string[] lines = comment.Split(
    new string[] {System.Environment.NewLine},
    System.StringSplitOptions.RemoveEmptyEntries)
      .Trim();
foreach(string line in lines)
    sb.Append(string.Format(@"""{0}"" ", line); //note blank space at the end of format string -- a delimiter
comment = sb.ToString();


Also, never repeat string concatenation "+". Strings are immutable — do I have to explain the consequence? Especially, in your case you already use string.Format.

Could be:
C#
tagProcess.StartInfo.Arguments = string.Format(@"{0} ""{1}""",  tag, comment);


However, you don't need those quotation marks. Each argument optionally goes in quotation marks, not all of them together. I already put those marks in the previous code sample.

—SA
 
Share this answer
 
v3
Comments
Simon Bang Terkildsen 30-Aug-11 16:06pm    
+5
Sergey Alexandrovich Kryukov 30-Aug-11 16:19pm    
Thank you, Simon.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900