【问题标题】:how to pass argument to a process如何将参数传递给进程
【发布时间】:2012-05-27 18:34:49
【问题描述】:

有没有办法将字符串参数传递给从我自己的进程产生的进程。

我的主应用程序中有:

Process.Start(Path.Combine(Application.StartupPath, "wow.exe");

wow.exe 是我创建的另一个应用程序。我需要将参数传递给这个 exe(一个字符串)。我通常怎样才能做到这一点?

我尝试了什么:

 ProcessStartInfo i = new //........
 i.Argument = "cool string";
 i. FileName = Path.Combine(Application.StartupPath, "wow.exe");
 Process.Start(i);

wow 应用程序的主体中,我写道:

static void Main()
{
    //print Process.GetCurrentProcess().StartInfo.Argument;
}

但我从来没有在第二个应用程序的 Main 中找到我的字符串。 Here is a question 询问why,但没有how to solve it..

编辑: Environment.GetCommandLineArgs()[1],必须如此。尽管如此,让它工作。接受@Bali 的回答,因为他首先提出了这个答案。谢谢大家

【问题讨论】:

    标签: c# .net-4.0 process arguments main


    【解决方案1】:

    要让参数传递,您可以在Main 中使用string[] args,也可以使用Environment.GetCommandLineArgs

    例子:

    Console.WriteLine(args[0]);
    

    Console.WriteLine(Environment.GetCommandLineArgs[0]);
    

    【讨论】:

      【解决方案2】:

      你可能想要一个

      static void Main(string[] args)
      {
      }
      

      其中args 包含您传入的参数

      【讨论】:

      • 我试过了,但我仍然在 main 中得到 Process.GetCurrentProcess().StartInfo.Argument; 的空字符串
      • 你不需要那个。试试Console.WriteLine(args[0].ToString())
      【解决方案3】:

      这是一个如何将参数传递给 exe 的示例:

      static void Main()
      {
         string[] args = Environment.GetCommandLineArgs();
      
         string firstArgument = args[0];
         string secondArgument = args[1];
      }
      

      或者稍微改变一下你的主要方法:

      static void Main(string []args)
      {}
      

      【讨论】:

        【解决方案4】:

        在你的wow.exeprogram.cs

        static void Main()
        {
             //Three Lines of code 
        }
        

        改成

        static void Main(string[] args)
        {
             //Three Lines of code 
        }
        

        string[] args. 现在将包含您传递给 exe 的参数。

        或者你可以使用

        string[] arguments = Environment.GetCommandLineArgs();
        

        你的论点被空格" "打断了。

        【讨论】:

        • 我在 Main() 中看不到任何类似的东西。
        猜你喜欢
        • 2018-09-17
        • 1970-01-01
        • 2011-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-16
        • 2012-11-07
        • 2012-07-05
        相关资源
        最近更新 更多