【问题标题】:Process.Start - Pass html code to exe as argumentProcess.Start - 将 html 代码作为参数传递给 exe
【发布时间】:2010-04-09 21:24:39
【问题描述】:

我正在使用下面的代码从 Windows 服务启动可执行文件,我需要将 html 代码(存储在变量中)作为参数传递。我用双引号转义,但这不起作用。我需要做什么才能正确通过这个?提前感谢您提供的任何指导。

服务内部:

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”可以包含任何内容、div、图像、超链接等。而且 html 代码可能很长。我不应该尝试将这么多数据作为参数传递吗?再次感谢您的帮助。

【问题讨论】:

  • 是的,发布 dr["rec"]、htmlVar 和主题的示例值,因为这会产生很大的不同。使用编辑按钮添加您的更改。

标签: c# argument-passing


【解决方案1】:

您可能需要对以下字符进行编码以使其可在命令行参数中传递:

  • 双引号
  • 回车
  • 换行

【讨论】:

  • 空格也会导致问题。
【解决方案2】:

注意不要在命令行传递太多:

我认为 2000 多个字符开始变得太长了。

【讨论】:

    【解决方案3】:

    来自 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 变量中可能存在的字符。

    【讨论】:

    • 您传递的是 HTML 还是 URL 并不是 100% 清楚的。如果要传递 HTML,则必须将 htmlvar 用引号括起来。而且,请记住,Process.Start 将像从命令行一样运行此进程。如果引号在命令行中不起作用,则在 Process.Start 中不起作用
    • 也许你意识到了这一点,但发帖人根本没有提到 Internet Explorer。 MyApp.exe 是否可以处理 html,而不是 url/路径,取决于 MyApp.exe 的实现。尽管您可能会向发布者推荐他尝试不同的方法,假设他有能力更改 MyApp.exe 的实现?干杯
    • @AaronLS:这是从 MSDN 文档中复制/粘贴的,用于演示将变量传递给他的 *.exe。如您所见,您可以从命令行调用:IExplore.exe "C:\myPath\myFile.htm" 而 Process.Start 显然会为您处理路径引用。我的观点是,如果htmlVar 包含引号,那么他依靠 Process.Start 将第二个参数包含在引号中的方法将不起作用。
    • 感谢您的回复。 @AaronLS/@Jim:你们俩都是正确的,因为我已经省略了 htmlVar 的右引号,但是在更正之后,我仍然无法将 html 作为参数传递。即使是像“
      test
      ”这样的简单字符串也不会正确通过,因为它只是被丢弃了。我尝试使用 String.Replace('"', '\"') 认为是引号导致它失败,但这也无济于事。有什么建议吗?
    【解决方案4】:

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

    变成

    "SomeSubject" "SomeHTMLVar

    请注意,没有结束引号。也许你想要这个:

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

    【讨论】:

      最近更新 更多