I have a .txt file like this:
6 4
1 2
2 3
3 4
4 5
1 2 4 5
How can I use this as command line argument in C#?
I have a .txt file like this:
6 4
1 2
2 3
3 4
4 5
1 2 4 5
How can I use this as command line argument in C#?
If you intend to give your program data program.exe < data.txt
, this is called reading from standard input. You can do this via .NET's Console.OpenStandardInput with
new StreamReader(Console.OpenStandardInput())
Alternatively, if you'd rather your program be run program.exe data.txt
, start with
void Main(string[] args)
{
File.ReadLines(args[0])
}
You can accept the file name in the command line (along with path) and open the file in your application, read it line by line and process all the lines.
Go to Solution Explorer
> Right click on project file
> Select Properties from context menu
. This opens the properties window of the project. Now Go to Debug tab
> Go to Start options section
in right pane. Put the full file path inside Command line arguments
text box as shown below:
< "D:\Rasik\input01.txt"
Then you can write code as usual and you will start getting input from the file:
class Program
{
static void Main(string[] args)
{
var textInFirstLineOfFile = Console.ReadLine();
}
}