0

Java では、バッチを介してプログラムにパラメーターを渡すことができます。C#でそれを行うにはどうすればよいですか?

たとえば、プログラムがファイル名を受け取る必要がある場合、どうすればそれをプログラムに取得できますか?

4

4 に答える 4

3

C# コンソール アプリケーション (exe) を作成したと仮定すると、文字列の配列を受け取るメインの静的メソッドを使用して作成されます。これらの文字列は、プログラムに渡される引数になります。

例えば:

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

コンソール アプリケーションの名前が「MyApp.exe」の場合、次の方法でパラメーターを渡すことができます。

MyApp.exe "最初の引数" 2 番目

そして、次の出力が得られるはずです。 出力

于 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

メインの外で使用できます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 に答える