Custom CMD Commands - c#

Is it possible to create custom Command Prompt parameters for an application written and compiled in Visual Studio C#. I want the users of my application to be able to do stuff in it just by command line.
For example, if the user types in CMD application_name.exe -parameter to do the code that was assigned to parameter.

Yes, this is what string[] args is for in Program.cs.
The easiest way to ensure you have everything you need is to create your application from the Console Application template in Visual Studio.
To do this:
Go to File -> New Project...
Select Console Application from the list of available templates
Name your Solution
Hit OK to finish creating the Project
This will ensure you have all the references needed and that the Program.cs class is setup properly.
Here is a psuedo example of what you can do in the Main method of your program in order to accept parameters at the command line:
static void Main(string[] args)
{
if (args.Length <= 0) //Checking the Length helps avoid NullReferenceException at args[0]
{
//Default behavior of your program without parameters
}
else
{
if (args[0] == "/?")
{
//Show Help Manual
}
if (args[0] == "/d")
{
//Do something else
}
//etc
}
}
Then at the command prompt the user can type:
yourProgamName.exe /?
yourProgramName.exe /d
As mentioned in an answer that was removed, there is a great library for handling complex Command Line options Here:
NDesk Options

That is what the "args" parameter is for in your main method.
If your users run "application_name.exe -parameter" then args[0] will be "-parameter". You can use this to branch your application to the correct code.
Check out this question to see examples and good libraries for handling lots of command line parameters/arguments.

and if you don't have access to you main() for some reason, you can get your command line from Environment.CommandLine.

Related

Start WPF application with arguments

I'm trying to implement a way for me to start my WPF application with specific arguments through the Windows task schedular and CMD. I've added the code below.
protected override void OnStartup(StartupEventArgs e)
{
Logger.Info(e.Args.Length);
for (int i = 0; i != e.Args.Length; ++i)
{
if (e.Args[i] == "test")
{
Logger.Info($"G");
}
else
{
Logger.Info($"B");
}
}
}
When I start publish the application and start it through CMD or schedule it in task schedular with arguments, the e.Args.Length is 0. But when I add an argument in Properties > Debug > Command line arguments, it does work.
Any idea what I'm missing?
According to the Comments of the Question i assume the problem is that the Start-Arguments are passed wrong. A ClickOnce Application doesent work like a .exe File and can not be started with parameters from the CMD due to security reasons
(Source)
I suggest you take a look at this, it describes nicely how arguments can be passed to the application via query strings.
You need to use Environment class in System Namespace and use GetCommandLineArgs() method to retrieve the arguments.
For example,
Args = Environment.GetCommandLineArgs();
For runining the application form cmd with command arguments, use below format -
C:\StackOverflow\Bin\Debug> StackOverflow.exe arg1 arg2

Command line arguments using in Windows form

I have C# Windows form application, I am doing a lot of check boxes and write and read them to JSON format file called test.json. Now I want to use my test.json with program.exe, that my program.exe would get check box checked as written in test.json. So I create Load event handler and want to use my GetCommandLineArgs().
private void Form1_Load(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
}
I know that I have 2arguments.[0] - program.exe and [1] - test.json. Need some ideas how to make it work.
My question is:
How to make that args list of 2elements. Where 0 is program.exe and 1 is test.json. And how to work with args.length when there is possibility that there will be no parameters or only one parameter
How to make that args list of 2 elements. Where 0 is program.exe and 1 is test.json
To do this you can debug from within visual studio by opening the project properties window and navigating to the Debug tab, from within there you can specify the command line args you want as shown below:
You can use the Environment.GetCommandLineArgs(), it is a little odd in my opinion. It provides to you the executable name that is currently running. So in your case you would end up with exactly what you're asking for.
[0] - program.exe and [1] - test.json
How to work with args.Length when there is possibility that there will be no parameters or only one parameter.
This is simple, the Environment.GetCommandLineArgs() returns a string[]. Check for the desired length and handle it accordingly.
var commandLineArgs = Environment.GetCommandLineArgs();
if (commandLineArgs.Length > 0)
{
// Do something magical...
}
else
{
// Nothing was passed in...
}
Something like this:
var cmdArgs = Environment.GetCommandLineArgs();
if (cmdArgs.Length < 2)
{
MessageBox.Show("No JSON file specified!");
}
var jsonFilename = cmdArgs[1];
If you do more complex command line parameter parsing I suggest to use an existing library like this one.
Update:
Here is where you can attach your event-handler (or create a new one doing a double-click):

How can I read the command line supplied to an offline clickonce application?

I have a simple application that opens a TCP connection and communicates via Telnet to another system. The application is to read a file that contains parameters and a simple scripting language to issue commands based on prompts from the connection.
Written in VS2013 using .NET 4
My application works as designed with one little exception.
I am publishing to a location using VS2013 which works well enough but the idea is to read a command line passed to my application that contains the path/file for the script to execute and that doesn't work as expected.
Finding out the hard way, the standard args[] parameters are not passed when it's published this way.
I have searched out multiple solutions that don't work both on here and other sites.
This is the basis (excerpt from page) of my current implementation to read the command line (found here: http://developingfor.net/2010/06/23/processing-command-line-arguments-in-an-offline-clickonce-application/). This seems to be similar to all solutions I've found, each with some variation that doesn't work.
string[] args = null;
if (ApplicationDeployment.IsNetworkDeployed)
{
var inputArgs = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData;
if (inputArgs != null && inputArgs.Length > 0)
{
args = inputArgs[0].Split(new char[] { ',' });
}
}
else
{
args = e.Args;
}
This SHOULD return args[] with parameters passed. I believe it would also include the actual command with path to the application. The Split function is because the author wishes to pass arguments separated by commas and not spaces.
My incarnation of this is a bit longer to include some checks to see if we actually get arguments from being compiled as an exe instead. If I compile to EXE and supply a command line, all is fine. Here is my code, not very concise as I've made lots of changes to debug and make this work.
I haven't figured out how to debug in the ide as network deployed with a command line so my debug code is via messagebox.
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0) //If we don't have args, assume onclick launch
{
if (ApplicationDeployment.IsNetworkDeployed) //are we oneclick launched?
{
var cmdline = AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData; //this should hold the command line and arguments????
if (cmdline != null && cmdline.Length > 0) //we have something and it contains at least 1 value
{
//This is all debug code to see what we get since we can't trace in this mode
MessageBox.Show(cmdline.Length.ToString()); //how many objects do we have?
foreach (String s in cmdline)
{
MessageBox.Show(s); //show us the value of each object
}
Application.Run(new frmMain(args)); //launch the form with our arguments
}
else
{
//quit application
MessageBox.Show("No command line.1"); //debug so we know where we failed
Application.Exit();
}
}
else
{
//quit application
MessageBox.Show("No command line.2"); //debug so we know where we failed
Application.Exit();
}
}
else
{
Application.Run(new frmMain(args)); //launch form with args passed with exe command line
}
}
Running the code above like this:
sTelent.application 1234
I have also explored the URL passing method which seems to only apply if launched from a web server, which this application is not.
At first I got NULL for my object:
AppDomain.CurrentDomain.SetupInformation.ActivationArguments.ActivationData
After more research I discovered that in my project properties under the Publish section there is an option button and under Manifests I can choose "Allow URL Parameters to be passed to application"
I checked this box and while I get different behavior, I don't get the desired behavior.
With that option checked I now get 2 messages boxes: The first showing the number of objects in cmdline and that number is 1 and the second showing the value of that one object which contains only the path/command to my application. No other objects and definitely not my arguments.
Am I totally off base? How do I get my command line arguments from an offline clickonce published application?
It seems that you must put the argument on the .appref-ms and not on not the .application or .exe for this to work correctly for clickonce based applications.
I created a short cut on my desktop by copying the installed application link found under All Programs. That should create an icon on your desktop with the same name as your application.
Then, open a command prompt, type in “%userprofile%\Desktop\My App Name.appref-ms” word for word (of course replace "my app name" with your application name). It should then pass the arguments. You can also put the command within a .bat file. I'm sure that you can also reference the link directly, it typically is located under c:\users[user profile]\appdata\roaming\Microsoft\windows\start menu\programs[app name]

Running exe from command line with parameters

I have created a WPF application which I would like to run from the command line, so I can schedule this command to be executed using Windows Task Scheduler.
For example, using a command line:
start "App.exe" "ID=1"
My questions is, how do I configure my WPF application to handle a call like this and is this the right syntax I should be using from making the call from the command line.
In a WPF application, you can access the command line by using the static members of the Environment class...
public MainWindow()
{
var args = Environment.GetCommandLineArgs();
if (args.Length == 1)
{
MessageBox.Show("No argument provided");
Environment.Exit(0);
}
string arg1 = args[1]; // your argument
InitializeComponent();
}
This snippet shows how to do it. Remember that the name of the assembly is always the first argument, so you are interested in args[1] and args[2] etc etc.
The Environment class also has another member: Environment.CommandLine which has the entire command line as a string.
For your second question, your syntax is fine.

Strange XML modification when input and output it in console app using Visual Studio

I have a simple console app that was creted in c# using VisualStudio. It has a input parameter and return it to output.
This is the code:
static void Main(string[] args)
{
string msg = args[0];
Console.WriteLine(msg);
}
Then I try to pass a param that contain a xml as string: "<messages><message>message</message></messages>". There is nothing wrong if I will use console for calling application. But in the case when I trying to debug such application I have added parameter string to Command line arguments on the Debug tab.
After this I have got unexpected output like:
^<messages^>^<message^>message^</message^>^</messages^>
Why we have such output and how to overcome this? Thanks in advance.
The simplest solution for this issue should be replacing unwilling characters like here:
static void Main(string[] args)
{
string msg = args[0];
Console.WriteLine(msg.Replace("^","");
}
But the reason of such behavior placed somewhere in the core of the operation system. Windows handle this situation like launch a batch file from command line with parameters. Following the link that was presented in the answer of #Artem K. you can find out that here is two way to overcome such situation. Unfortunately, no one work with this issue. Possible, because Visual Studio add something from yourself in the question how to pass args for launching batch.
How to avoid cmd.exe interpreting shell special characters like < > ^
in Shell, the < and > has special meanings concidering steam redirection..
what makes dir /w > myDirectory.txt possible or tree | more Etc...
Follow #Artem Koshelev post to get around that.

Categories