I am currently writing a console application that deletes old log files, now I got that functionality working, I was trying to figure out how to execute a program with parameters from Command Prompt.
Example:
FileDeleter.exe days 3
where I'm running the program and telling it to delete 3 days worth of log files. Can these arguments be passed as variables into the code? I am not sure how this is accomplished.
Thanks for the help!
ArthurA
If you need more advanced parsing, there are of course libraries available
Best way to parse command line arguments in C#?
https://github.com/BizArk/BizArk3
http://fclp.github.io/fluent-command-line-parser/
your void main should take an array of strings as an arg:
static void Main(string[] args)
You can then parse the args as you see fit.
As #Neil N already mentionend, you have to define a your main method like this:
static void Main(string[] args)
args will then contain the arguments you passed.
If you run your program like this: FileDeleter.exe days 3, args[0] would contain the string "days" and args[1] would contain the string "3". Pay attention here that the latter one will be a string despite the fact that you passed a number. Therefor you might have to parse it to a number before using it.
Related
This question already has answers here:
Best way to parse command line arguments in C#? [closed]
(20 answers)
Closed 3 years ago.
I have a C# program called data-1 that I run on Mac OS
Where I run this I use:
> dotnet data-1.dll
How can I change the main so that I can enter something like
> dotnet data-1.dll 10, 20
and pass into the code the numbers 10 and 20?
static void Main(string[] args)
{
When you pass command line arguments to your program they're contained in the args parameter of your main function.
You can then access each argument through an index.
If you call for example: dotnet data1.dll 10 20 args[0] would be 10 and args[1] would be 20.
Just remember that all command line arguments are initially parsed as a string so you would have to convert these string values to int or another type.
You can not pass integers in, only strings. That is a dos era limit on the command line and Commandline Arguments.
But parsing strings into a type like Int, is kinda the most important part of the UI work. And .NET has extensive support for it. So a simple call to Int32.Parse() will solve this.
However parse throws a vexing amount of exceptions on any parsing problem. So it is usually better to use TryParse() instead, even if the pattern for use is a bit harder to learn/more complex.
This question already has answers here:
How do I use command line arguments in my C# console app?
(2 answers)
Closed 3 years ago.
In the program i need to have a way of reading a command line thats given to the program with the running of the program. so for example:
"flying-postman.exe mail.txt boeing-spec.txt 23:00 –o itinerary.txt"
so this line should firstly run the program(which is flying-postman.exe) and then it also needs to feed those 4 following variables to the program.
So i know that ReadLine() exists, but it wont be helpful in the case because i need it to take those variables at the same time the program is called not after the program is called. i.e i dont want it to run the program then wait for the user to enter in the values.
The entry point to a console app is main, which takes an array of strings, something like
class Program
{
static void Main(string[] args)
{
}
}
If you call your program with some arguments, the first will be the exe name, and the rest your strings.
I started to learn C# a week ago and I'm getting familiarized with the working environment. So far we've learned the usual stuff: variable types, function declarations, how to compile a project, etc.
As the first assignment our teacher gave us a screen capture of how we are supposed to pass arguments to a executable file.
I read thatstring[] args is what is used for "grabbing" from the console and passing on to the rest of the code. However when I try to print like this:
Console.WriteLine(args);
I always get the same result:
How can I pass a parameter to the exe file via the console?
The console is outputting the entire string array object as a string (System.String[]). To see its contents you need to iterate through the array:
foreach (string s in args)
{
Console.WriteLine(s);
}
This will show you the contents of the array. The [0] value will always be the name of the executable, and your parameters will start at position [1].
I have a FORTRAN .exe file which runs and works ok, it will ask
user to input 1 or 2 and if 1 is entered it will do some calculation and if 2 is entered it does different kind of calculation.
I need to call this from C# code. I know how to run .exe file from C# but I can not pass 1 or 2 to the .exe
I have used different method but with no luck.
static void Main(string [] args)
{
string FileName = #"C:\......sco.exe";
process.StartInfo = new ProcessStartInfo(FileName,"3");
Process.Start(process.StartInfo); }
I really appreciate if some one knows how to fix this problems. I am new to C# and I can not rewrite the Fortran code since it is too comelicated.
Thank you for reading this post
I don't know C#, so I can't tell you how to do this in detail, but when running a fortran program from the command line, you can supply an extra file with arguments. Call it like this: mypgrogram.exe<inputs.ans
In your case, inputs.ans would contain a single 1 or 2. You can put each additional argument that the program asks for on a new line in this file.
I have a program that calculates the factorial of multiple numbers. These nubers are passed as parameters in cmd as such :
factorial.exe 3 4 5
This will calculate the factorials of 3, 4 and 5 respectively.
An earlier version of the program had a percentage that showed the fullness of the stack. I want to bring that back now, but I to also pass the wait time as a parameter in cmd as such:
factorial.exe 3 4 5 wait_time1
or
factorial.exe 3 4 5 1000
To read the numbers I use this args parser :
static string the_time;
public static void Main(string[] args)
{
foreach (string s in args)
{
extra = int.Parse(s);
Calculate(extra);
}
}
How can I separate the arguments?
Thanks in advance.
You could add the waittime arg like a switch /t:100 so only when you see /t you know it is a waittime.
If you know your args will always have the waittime, then waittime is
waittime = arg[args.Length-1]
Probably worth a look as well
Best way to parse command line arguments in C#?
In case you don't want to reinvent the wheel
http://commandline.codeplex.com/
http://www.codeproject.com/Articles/3852/Automatic-Command-Line-Parsing-in-C
I think, what you want is something like getopt on UNIX-like machines to parse arguments in a sensible way. Have a look here.
Each element at the array will be a string from the command line, space-divided. This way, for
factorial.exe 3 4 5
will you have
args[0] //3
args[1] //4
args[2] //5
EDIT
Thanks to DD59, now I understand your question. You could have a convention that the last parameter will always be the wait time, or you could use a syntax, such as -time (or /t:, as he said).
factorial.exe 3 4 5 -1000
Regards
You are already separating the arguments in foreach loop. The string s is what you get as argument. I suppose you want to pinpoint the wait_time value and don't want to calculate it of course. You have to fix the position like - last argument is wait_time or first arugment is. Without fixing the location it will be hard to determine.
Is wait_time a mandatory argument? Does it always stand at the last place? Then you could do it like this:
the_time = args[args.Length - 1];
for (int i=0; i<args.length - 1; i++){
extra = int.Parse(args[i]);
Calculate(extra);
}
If wait_time is optional, you should go with DD59's answer.
Parsing the command line is a quite complex part. Maybe you should take a look at a available library (some examples):
http://commandline.codeplex.com/
https://github.com/mono/mono/tree/master/mcs/class/Mono.Options/