Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

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 237 views
1 answer 157 views
2 answers 234 views
2 answers 183 views
183 views asked May 1, 2021 by avibootz
...