2010-04-09 99 views
4

我正在使用下面的代碼從Windows服務啓動可執行文件,我需要傳遞HTML代碼(存儲在變量中)作爲參數。我用雙引號轉義,但這不起作用。爲了正確傳遞,我需要做些什麼?預先感謝您提供的任何指導。Process.Start - 將html代碼傳遞給EXE作爲參數

裏面的服務:

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

,然後內MyApp.exe的:

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

EXE文件僅僅是一個簡單的應用程序,處理電子郵件的發送。 dr [「rec」]。ToString()是收件人的電子郵件地址。變量「主題」將包含電子郵件的主題。變量「htmlVar」可以包含任何東西,divs,圖像,超鏈接等等,而且html代碼可能相當長。我不應該試圖將這麼多數據作爲參數嗎?再次感謝您的幫助。

+2

什麼是HTML? – ChrisF 2010-04-09 21:25:58

+0

是的,發佈dr [「rec」],htmlVar和主題的示例值,因爲這會產生很大的差異。使用編輯按鈕添加您的更改。 – AaronLS 2010-04-09 21:39:05

回答

5

您需要編碼如下字符,使他們在命令行中的參數差強人意:

  • 雙引號
  • 回車
  • 換行符
+1

空間也可能導致問題。 – 2010-04-22 17:47:25

2

來自MSDN文件:http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx

// Opens urls and .html documents using Internet Explorer. 
    void OpenWithArguments() 
    { 
     // url's 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"); 
    } 

編輯: AaronLS讓你更清楚你想要完成什麼。 傳遞多個參數

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(); 

下面的方法是從MSDN頁面採取的ProcessStartInfo參數: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('\\'); 
     } 
    } 

這不是最有效的解決您的問題,但我只是複製代碼,以便你可以看到如何正確地轉義可能存在於你的htmlvars變量中的字符。

+0

如果您傳遞HTML或URL,這並非100%清楚。如果您傳遞HTML,則必須將htmlvar包含在引號中。並且,請記住,Process.Start將像從命令行那樣運行此過程。如果引號不能從命令行運行,它將無法從Process.Start – 2010-04-09 21:37:26

+0

工作。也許你重新實現了這一點,但海報並沒有對Internet Explorer做任何說明。 MyApp.exe是否可以處理html,而不是URL的/路徑,取決於MyApp.exe的實現。雖然也許你正在向海報推薦他嘗試一種不同的方法,假設他有能力更改MyApp.exe的實現?歡呼 – AaronLS 2010-04-09 21:41:47

+1

@AaronLS:這是從MSDN文檔複製/粘貼來演示傳遞變量到他的* .exe。正如你所看到的,你可以從命令行調用:'IExplore.exe「C:\ myPath \ myFile.htm」',而Process.Start顯然會爲你處理引用的路徑。我的觀點是,如果'htmlVar'包含引號,那麼依賴Process.Start將第二個參數包含在引號中的方法將不起作用。 – 2010-04-09 22:15:35

1

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

變爲

"SomeSubject" "SomeHTMLVar

公告不存在收盤報價。也許你想這樣的:

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