Process.Start - pass exe html code as argument

I use the code below to run the executable from a Windows service, and I need to pass the html code (stored in the variable) as an argument. I avoid double quotes, but this does not work. What do I need to do to pass this correctly? Thanks in advance for any recommendations that are offered.

Inside the service:

Process.Start(@"E:\Program Files\MyApp.exe", dr["rec"].ToString() + " \"" + subject + "\" \"" + htmlVar); 

and then in MyApp.exe:

 static void Main(string[] args) { Program MyProg = new Program(); MyProg.MyMeth(args[0].ToString(), args[1].ToString(), args[2].ToString()); } 

An exe file is a simple application that handles sending emails. dr ["rec"]. ToString () is the email address of the recipient. The variable "subject" will contain the email subject. The variable "htmlVar" can contain anything, divs, images, hyperlinks, etc., And the html code can be quite long. Should I not try to pass this data as an argument? Thanks again for the help.

+4
source share
4 answers

You may need to encode the following characters to make them skipped in the command line argument:

  • Double quotes
  • Carriage return
  • Line channels
+5
source

Be careful not to jump too much on the command line:

I think 2000+ characters start too long.

+4
source

From the MSDN documentation: http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx

  // Opens urls and .html documents using Internet Explorer. void OpenWithArguments() { // url are not considered documents. They can only be opened // by passing them as arguments. Process.Start("IExplore.exe", "www.northwindtraders.com"); // Start a Web page using a browser associated with .html and .asp files. Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm"); Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp"); } 

Edit: AaronLS clarified a bit what you are trying to accomplish. to pass multiple arguments

 Process myProcess = new Process(); string arg = String.Format("{0} {1}{2}{1} {1}{3}{1}", dr["rec"], '"',htmlVar); myProcess.StartInfo.FileName = @"E:\Program Files\MyApp.exe"; myProcess.StartInfo.Arguments = ArgvToCommandLine(new string[] { arg }); myProcess.Start(); 

The following methods were taken for the ProcessStartInfo arguments on the MSDN page: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments.aspx

  public static string ArgvToCommandLine(IEnumerable<string> args) { StringBuilder sb = new StringBuilder(); foreach (string s in args) { sb.Append('"'); // Escape double quotes (") and backslashes (\). int searchIndex = 0; while (true) { // Put this test first to support zero length strings. if (searchIndex >= s.Length) { break; } int quoteIndex = s.IndexOf('"', searchIndex); if (quoteIndex < 0) { break; } sb.Append(s, searchIndex, quoteIndex - searchIndex); EscapeBackslashes(sb, s, quoteIndex - 1); sb.Append('\\'); sb.Append('"'); searchIndex = quoteIndex + 1; } sb.Append(s, searchIndex, s.Length - searchIndex); EscapeBackslashes(sb, s, s.Length - 1); sb.Append(@""" "); } return sb.ToString(0, Math.Max(0, sb.Length - 1)); } private static void EscapeBackslashes(StringBuilder sb, string s, int lastSearchIndex) { // Backslashes must be escaped if and only if they precede a double quote. for (int i = lastSearchIndex; i >= 0; i--) { if (s[i] != '\\') { break; } sb.Append('\\'); } } 

This is not the most effective solution to your problem, but I just copied the code so that you can see how to correctly avoid the characters that may be present in your htmlvars variable.

+2
source

" \"" + subject + "\" \"" + htmlVar

becomes

"SomeSubject" "SomeHTMLVar

Please note that the closing quote is missing. Perhaps you wanted this:

" \"" + subject + "\" \"" + htmlVar + "\""

+1
source

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


All Articles