Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Loading a file from a command line?

I am relatively new to C# and I am having a little trouble.

I am creating a program where I want to load a file from the command line. For example:

MyProgram.exe C:\ExcelDocument.xls
like image 969
buzzzzjay Avatar asked Aug 26 '26 23:08

buzzzzjay


1 Answers

in the Main method of your program the args string array parameter to the method will contain any command line parameters. The args array will contain 1 value for each space separated element that is not enclosed in quotes (")

so

myprograme.exe c:\my documents\file1.xls 

will result in 2 args:

c:\my
documents\file1.xls

whereas

myprograme.exe "c:\my documents\file1.xls"

will result in 1 value in args:

c:\my documents\file1.xls

you can access the params via the indexer:

string file = args[0];

assuming that the file is the first argument.

obviously you will still need to load the actual file, this will only give you the name give as a parameter to your program.

like image 181
Sam Holder Avatar answered Aug 29 '26 14:08

Sam Holder