I wish to make a command-line file searching program in C#. I want it to have a callable name such as 'search' as well as command-line arguments such as 'search name'. How might one implement this in C#? I figure I will have to create a new command line command, but I don't know how parameters might be passed to C# methods.
Asked
Active
Viewed 650 times
-1
-
1There are several good nuget packages out there for parsing command line arguments. Regardless, you need to be more specific about the issue you are encountering. – BradleyDotNET Jan 16 '19 at 20:26
-
I thought I was pretty specific. Callable name, passable commands. – EpicNicks Jan 16 '19 at 20:30
-
Or, as an off-the-wall alternative, you could create one (or a set of) PowerShell _Cmdlets_ in a "module". Powershell handles aliases, command completion, intelligent "pipelining", etc. much better than cmd.exe. – Flydog57 Jan 16 '19 at 20:48
-
A sample usage? Sample code? "How to use command line arguments to do an unspecified thing" is fairly broad. Apparently you were just looking for the `args` array, I assumed you had at least gotten that far. – BradleyDotNET Jan 16 '19 at 21:46
-
That wasn't all. I mentioned how one would make a cmd/PowerShell command to execute with those arguments. As crazy as it might sound, they never really taught me the full meaning of public static void Main(String[] args), and the internet doesn't directly either. – EpicNicks Jan 17 '19 at 03:02
2 Answers
0
Parameters passed from consoles can be accessed if the Main method has parameter string[] args
. Alternatively, this can be accessed using the method System.Environment.GetCommandLineArgs()
. Unlike C/C++, this does not include the name of the program.
Here is a question on adding aliases: Aliases in Windows command prompt This would enable you to type 'search' instead of search.exe.

Gnbrkm41
- 238
- 3
- 9
-
So if I entered the alias followed by a string, that string would be passed into the args[] array? – EpicNicks Jan 16 '19 at 20:35
-
@EpicNicks Say, if you typed "search.exe FileName -a -b" into the cmd, `args` would be { "FileName", "-a", "-b"}. – Gnbrkm41 Jan 16 '19 at 20:39
-
About to try it. Didn't know that was even a thing. Cool. Here's a digital gold star. – EpicNicks Jan 16 '19 at 20:40
0
For debugging you can set command line arguments in the project properties.
Then simply access the arg parameter in the main event (for a console app).

Neil B
- 2,096
- 1
- 12
- 23