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.
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.
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 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.
This question already has answers here:
Why Windows sets new created file's "created time" property to old time?
(3 answers)
Closed 5 years ago.
I am attempting to get the creation date of a file. I am using the File.GetCreationTime() method to do this. If the file is a new file, it seems to work fine. If I delete the file though and re-create it, it seems to be giving me the original creation time. Since the file was deleted, it seems weird and even impossible that it is returning the original date and time of the file.
I put together a simple console application to demonstrate the issue:
static void Main(string[] args)
{
const string fileName = #"C:\Temp\dummy.txt";
File.AppendAllText(fileName, "This is a test");
DateTime creationDate = File.GetCreationTime(fileName);
Console.WriteLine(creationDate.ToShortDateString() + " " + creationDate.ToShortTimeString());
System.Threading.Thread.Sleep(120000);
File.Delete(fileName);
File.AppendAllText(fileName, "This is a test");
creationDate = File.GetCreationTime(fileName);
Console.WriteLine(creationDate.ToShortDateString() + " " + creationDate.ToShortTimeString());
}
This program creates a dummy file and appends the text This is a test. It then prints out the creation date and time to the console screen. So far, so good. It then sleeps for 2 minutes. After the 2 minutes have elapsed, it deletes the file and re-creates it. It then, again, prints out the creation date and time to the console screen. I would except the latter output to be 2 minutes later than the original, however, it is pulling the same exact date and time! I have single stepped through the program and I can verify that it is, indeed, deleting the original file from the hard drive.
Actual Output
--------------
5/6/2017 10:25 AM
5/6/2017 10:25 AM
Expected Output
----------------
5/6/2017 10:25 AM
5/6/2017 10:27 AM
Can someone explain to me what is going on here and how to work around the issue?
From the MSDN page
NTFS-formatted drives may cache information about a file, such as file creation time, for a short period of time. As a result, it may be necessary to explicitly set the creation time of a file if you are overwriting or replacing an existing file.
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.