Java では、バッチを介してプログラムにパラメーターを渡すことができます。C#でそれを行うにはどうすればよいですか?
たとえば、プログラムがファイル名を受け取る必要がある場合、どうすればそれをプログラムに取得できますか?
Java では、バッチを介してプログラムにパラメーターを渡すことができます。C#でそれを行うにはどうすればよいですか?
たとえば、プログラムがファイル名を受け取る必要がある場合、どうすればそれをプログラムに取得できますか?
C# コンソール アプリケーション (exe) を作成したと仮定すると、文字列の配列を受け取るメインの静的メソッドを使用して作成されます。これらの文字列は、プログラムに渡される引数になります。
例えば:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(string.Join("\n", args));
}
}
コンソール アプリケーションの名前が「MyApp.exe」の場合、次の方法でパラメーターを渡すことができます。
MyApp.exe "最初の引数" 2 番目
そして、次の出力が得られるはずです。
アプリケーションの Main() ルーチンは、コマンド ラインで渡された引数を含む文字列の配列を受け取ります。
static void Main(string[] args)
{
foreach (string s in args)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
メインの外で使用できますEnvironment.GetCommandLineArgs()
。
string[] args = Environment.GetCommandLineArgs();
*.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();