0

在java中,您可以通过批处理将参数传递给程序。我怎么能在 C# 中做到这一点?

就像我需要程序来接收文件名一样,我怎样才能将它传递给程序?

4

4 回答 4

3

假设您创建了一个 C# 控制台应用程序 (exe),它将使用接收字符串数组的 main 静态方法创建。这些字符串将是传递给程序的参数。

例如:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(string.Join("\n", args));
    }
}

如果您的控制台应用程序名为“MyApp.exe”,您可以通过以下方式传递参数:

MyApp.exe“第一个参数”第二个

你应该得到这个输出: 输出

于 2013-08-02T05:50:24.987 回答
2

应用程序中的 Main() 例程接收一个字符串数组,其中包含在命令行中传入的参数。

    static void Main(string[] args)
    {
        foreach (string s in args)
        {
            Console.WriteLine(s);
        }
        Console.ReadLine();
    }
于 2013-08-02T05:43:05.447 回答
1

在 Main 之外,您可以使用Environment.GetCommandLineArgs().

string[] args = Environment.GetCommandLineArgs();
于 2013-08-02T06:37:11.297 回答
0

如果您尝试读取 *.bat 文件的输出,这将对您有所帮助..`

Process  thisProcess = new Process();
thisProcess.StartInfo.CreateNoWindow = true;
thisProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
thisProcess.StartInfo.WorkingDirectory = @"C:\Users\My User Name\Documents";
thisProcess.StartInfo.FileName = "ipconfig";
thisProcess.StartInfo.Arguments = "/h";
thisProcess.StartInfo.UseShellExecute = false;
thisProcess.StartInfo.RedirectStandardOutput = true;
thisProcess.Start();
thisProcess.WaitForExit();
//Output from the batch file
string myOutput = thisProcess.StandardOutput.ReadToEnd(); 
于 2013-08-02T06:09:05.443 回答