Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 传递多个参数的正确语法是什么_C#_.net - Fatal编程技术网

C# 传递多个参数的正确语法是什么

C# 传递多个参数的正确语法是什么,c#,.net,C#,.net,我有以下代码来启动命令行应用程序: private void LaunchCommandLineApp(string latestStudents, string latestTopics) { // Use ProcessStartInfo class ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false;

我有以下代码来启动命令行应用程序:

        private void LaunchCommandLineApp(string latestStudents, string latestTopics)
    {
        // Use ProcessStartInfo class
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = "ConsoleApplication2.exe";
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.Arguments = 

        try
        {
            // Start the process with the info we specified.
            // Call WaitForExit and then the using statement will close.
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
            }
        }
        catch
        {
            // Log error.
        }
    }

startInfo.Arguments=
行作为参数传递最新学生和最新主题的正确语法是什么?我已经尝试了我能想到的一切和一些,但我仍然不明白

参数
是一个字符串,毫无帮助地说它完全由目标应用程序解释。它确实说现在.NET应用程序将解释它,所以它实际上取决于您启动的进程

要知道如何使参数字符串对您尝试将其传递给的进程做正确的事情,唯一的方法是了解该进程如何处理其参数(如果需要进行实验,请尝试从命令行运行它)。大多数情况下,你可以期望它们之间用空格隔开。您可能只需要执行以下操作(假设C#6):

但这可能不起作用,这取决于这些变量中的内容。它们可能需要被引用,特别是当它们本身包含空格时


我真的不能给你一个明确的答案。

参数
是一个字符串,毫无帮助地说它完全由目标应用程序解释。它确实说现在.NET应用程序将解释它,所以它实际上取决于您启动的进程

要知道如何使参数字符串对您尝试将其传递给的进程做正确的事情,唯一的方法是了解该进程如何处理其参数(如果需要进行实验,请尝试从命令行运行它)。大多数情况下,你可以期望它们之间用空格隔开。您可能只需要执行以下操作(假设C#6):

但这可能不起作用,这取决于这些变量中的内容。它们可能需要被引用,特别是当它们本身包含空格时


我真的没有确切的答案。

这取决于解释参数的程序,但通常情况下,如果用空格分隔参数,则它们将作为字符串数组呈现给程序

例如,如果将参数字符串指定为:

startInfo.Arguments = "one two   three  \"fo ur\" \"\\\"fi ve\"\\\""
然后,如果程序是C#控制台应用程序,
Main(string[]args)
方法将接收一个
args
数组,如下所示:

args[0] == "one"
args[1] == "two"
args[2] == "three"
args[3] == "fo ur"
args[4] == "\"fi ve\""
请注意,在我的示例中,连续空格(如“2”和“3”之间的空格)被忽略


还要注意,围绕“four”的引号导致它作为单个参数传递


最后,请注意,如果要将引号作为参数的一部分传递,则必须使用反斜杠对其进行转义。当然,在C#中,您必须避开反斜杠和引号,因此在我的示例中,我必须编写更加笨拙的反斜杠和引号“

这取决于解释参数的程序,但通常情况下,如果用空格分隔参数,则它们将作为字符串数组呈现给程序

例如,如果将参数字符串指定为:

startInfo.Arguments = "one two   three  \"fo ur\" \"\\\"fi ve\"\\\""
然后,如果程序是C#控制台应用程序,
Main(string[]args)
方法将接收一个
args
数组,如下所示:

args[0] == "one"
args[1] == "two"
args[2] == "three"
args[3] == "fo ur"
args[4] == "\"fi ve\""
请注意,在我的示例中,连续空格(如“2”和“3”之间的空格)被忽略


还要注意,围绕“four”的引号导致它作为单个参数传递


最后,请注意,如果要将引号作为参数的一部分传递,则必须使用反斜杠对其进行转义。当然,在C语言中,您必须避开反斜杠和引号,因此在我的示例中,我必须编写更难处理的
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

例如,如果您在Windows中打开了一个命令窗口,并且要运行类似于
ConsoleApplication2.exe/help
,则将“/help”作为命令参数传入

因此,对于您的情况(这取决于ConsoleApplication2.exe的编码方式),您需要执行以下操作:

startInfo.Arguments = latestStudents + " " latestTopics;

…假设ConsoleApplication2.exe按该顺序接受这两个参数。

ProcessStartInfo参数作为字符串传递,类似于通过命令行运行ConsoleApplication2.exe

例如,如果您在Windows中打开了一个命令窗口,并且要运行类似于
ConsoleApplication2.exe/help
,则将“/help”作为命令参数传入

因此,对于您的情况(这取决于ConsoleApplication2.exe的编码方式),您需要执行以下操作:

startInfo.Arguments = latestStudents + " " latestTopics;
…假设ConsoleApplication2.exe按该顺序接受这两个参数。

示例:

        ProcessStartInfo startInfo = new ProcessStartInfo("argsecho.exe");
        startInfo.WindowStyle = ProcessWindowStyle.Normal;

        // Start with one argument.
        // Output of ArgsEcho:
        //  [0]=/a            
        startInfo.Arguments = "/a";
        Process.Start(startInfo);

        // Start with multiple arguments separated by spaces.
        // Output of ArgsEcho:
        //  [0] = /a
        //  [1] = /b
        //  [2] = c:\temp
        startInfo.Arguments = "/a /b c:\\temp";
        Process.Start(startInfo);

        // An argument with spaces inside quotes is interpreted as multiple arguments.
        // Output of ArgsEcho:
        //  [0] = /a
        //  [1] = literal string arg
        startInfo.Arguments = "/a \"literal string arg\"";
        Process.Start(startInfo);

        // An argument inside double quotes is interpreted as if the quote weren't there,
        // that is, as separate arguments. Equivalent verbatim string is @"/a /b:""string with quotes"""
        // Output of ArgsEcho:
        //  [0] = /a
        //  [1] = /b:string
        //  [2] = in
        //  [3] = double
        //  [4] = quotes
        startInfo.Arguments = "/a /b:\"\"string in double quotes\"\"";
        Process.Start(startInfo);

        // Triple-escape quotation marks to include the character in the final argument received
        // by the target process. Equivalent verbatim string: @"/a /b:""""""quoted string""""""";
        //  [0] = /a
        //  [1] = /b:"quoted string"
        startInfo.Arguments = "/a /b:\"\"\"quoted string\"\"\"";
        Process.Start(startInfo);
当你在谷歌上搜索某样东西时,你能找到的东西真是太棒了

是我用作上述代码示例源代码的链接。

示例:

        ProcessStartInfo startInfo = new ProcessStartInfo("argsecho.exe");
        startInfo.WindowStyle = ProcessWindowStyle.Normal;

        // Start with one argument.
        // Output of ArgsEcho:
        //  [0]=/a            
        startInfo.Arguments = "/a";
        Process.Start(startInfo);

        // Start with multiple arguments separated by spaces.
        // Output of ArgsEcho:
        //  [0] = /a
        //  [1] = /b
        //  [2] = c:\temp
        startInfo.Arguments = "/a /b c:\\temp";
        Process.Start(startInfo);

        // An argument with spaces inside quotes is interpreted as multiple arguments.
        // Output of ArgsEcho:
        //  [0] = /a
        //  [1] = literal string arg
        startInfo.Arguments = "/a \"literal string arg\"";
        Process.Start(startInfo);

        // An argument inside double quotes is interpreted as if the quote weren't there,
        // that is, as separate arguments. Equivalent verbatim string is @"/a /b:""string with quotes"""
        // Output of ArgsEcho:
        //  [0] = /a
        //  [1] = /b:string
        //  [2] = in
        //  [3] = double
        //  [4] = quotes
        startInfo.Arguments = "/a /b:\"\"string in double quotes\"\"";
        Process.Start(startInfo);

        // Triple-escape quotation marks to include the character in the final argument received
        // by the target process. Equivalent verbatim string: @"/a /b:""""""quoted string""""""";
        //  [0] = /a
        //  [1] = /b:"quoted string"
        startInfo.Arguments = "/a /b:\"\"\"quoted string\"\"\"";
        Process.Start(startInfo);
当你在谷歌上搜索某样东西时,你能找到的东西真是太棒了


是我用作上述代码示例源代码的链接。

请像命令提示符一样尝试:startInfo.Arguments=“latestStudents latestTopics";用空格分隔你的论点。这是一个链接,实际上是第一个,在谷歌搜索之后,你有ConsoleApplication2.exe的代码吗?如果是,那么看看应用程序期望传递的参数是如何传递的就很简单了。如果没有,那么您至少应该有说明如何将参数放在命令行上的文档(如果已经编程了任何方法)。如果没有这些信息,也没有失败的尝试,任何答案都是猜测。如果这些输入中的任何一个可能包含空格或引号字符,您可能希望了解如何处理充分转义输入所涉及的复杂性。请像命令提示符一样尝试:startInfo.Arguments=“latestStudents latestTopics”;用空格分隔你的论点。这是一个链接,实际上是谷歌搜索之后的第一个链接