How to get and use the command line arguments in C#

1 Answer

0 votes
using System;
using System.Diagnostics;

namespace Command_Line_Arguments
{
	class Class1
	{
		static void Main(string[] args)
		{
			int i = 0;
			foreach (string str in args)
			{
				Console.WriteLine(args[i++]);
			}
			Process proc = new Process (); 
			switch(args[0])
			{
				case "notepad" : 
						proc.StartInfo.FileName = @"Notepad.exe";
                        // you can write any file name or remove the argument
						proc.StartInfo.Arguments = "c:\\temp.txt"; 
						// proc.StartInfo.Arguments = args[1]; 
						proc.Start(); 
						break;
				case "calculator" : 
						proc.StartInfo.FileName = @"Calc.exe"; 
						proc.StartInfo.Arguments = ""; 
						proc.Start(); 
						break;
				case "fexplorer" : 
						proc.StartInfo.FileName = @"explorer.exe"; 
						proc.StartInfo.Arguments = ""; 
						proc.Start(); 
						break;
				default: Console.WriteLine("Arguments Error");
						break;
			}
		}
	}
}
// use this program like this:
// Command_Line_Arguments.exe calculator
// Command_Line_Arguments.exe fexplorer
// Command_Line_Arguments.exe notepad c:\temp.txt


answered Aug 6, 2014 by avibootz
edited Aug 6, 2014 by avibootz

Related questions

3 answers 267 views
1 answer 175 views
2 answers 258 views
2 answers 213 views
213 views asked May 1, 2021 by avibootz
...