Reputation: 17
In java you can pass parameters to the program through batch. How can I do that in C#?
Like if I needed the program to receive a file name how can I get it to the program?
Upvotes: 0
Views: 224
Reputation: 111810
Outside of Main you can use Environment.GetCommandLineArgs()
.
string[] args = Environment.GetCommandLineArgs();
Upvotes: 1
Reputation: 2311
If you are trying to read the output from *.bat files this will help you..`
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();
Upvotes: 0
Reputation: 13089
Supposing you have created a C# console application (exe), it will be created with a main static method that receives an array of strings. Those strings will be the arguments passed to the program.
For example:
class Program
{
static void Main(string[] args)
{
Console.WriteLine(string.Join("\n", args));
}
}
If your console application is named "MyApp.exe", you could pass parameters is this way:
MyApp.exe "first arg" second
And you should get this output:
Upvotes: 3
Reputation: 346
The Main() routine in your application receives an array of strings which contain the arguments which were passed in on the command line.
static void Main(string[] args)
{
foreach (string s in args)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
Upvotes: 2