How to supply command line arguements to a form on launch? - c#

I have a a simple GUI App, which need to perform a certain function on start-up, but not on manual launch from desktop or start menu. To incorporate this function, I thought of supplying command line arguments to the application, so that depending on the command line argument, we can differentiate between launch on start up and manual launch.
My Question is that how do I make sure that whenever the user clicks the applications icon on his desktop, the required command line arguments are given to the program.
I am using C# to program my application and want to run it on windows 7

The common design pattern for Windows applications which can be launched either at startup (i.e., automatically) or manually (i.e., when a user launches the application) is to pass a command line argument when the application is launched at startup but not when a user launches it manually. Why? Because you control the mechanism by which the application is started automatically (for example, because you create a registry key in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run). So, for example, you could add a value of "c:\program files\MyCompany\MyApp.exe" /Startup And then you could check for the startup argument in your code:
bool isStartupLaunch;
foreach (string arg in args)
isStartupLaunch |= (arg.toLower() == "/startup");
By contrast, you never really can control how a user is going to launch your application. Maybe they double click on a shortcut, but maybe they double click on your actual executable, or maybe they open a command prompt and launch that way. So you don't want to rely on getting a specific command line argument when the user launches your app. Much safer to look for the automatic launch, because you control how that is done, and therefore can control the command line arguments passed in.

Related

How to detect if an EXE file is started in a command-line interface [duplicate]

This question already has answers here:
Output to command-line if started from command line
(3 answers)
Closed 2 years ago.
I have a windows forms application with a working GUI. However I want the application to be able to be started and used in a command-line interface aswell as a regular GUI application. When the application is started in an CLI the GUI shall not be used an instead relevant information will be written to the CLI-application. What I need to do this is a way to detect if the application (its exe file) is started in a CLI-application such as the regular windows CMD or started in a normal fashion such as clicking the exe file in the file explorer or using a desktop shortcut.
Preferably the detection should be done in the main method of the application such as
static class Program
{
[STAThread]
static void Main()
{
//if(application not started in a CLI-application)
Application.Run(new DriverToolApplicationContext());
//else
//Console.writeLine("Application started in CLI-application");
}
}
How is this detection best implemented? I preferably do not want to put any arguments in the main method.
I think you'll struggle without arguments tbh; most apps I can think of that have this dual mode operation bundle as two exes, a gui one and a command line one (and one maybe uses the other)
It really is the simplest way to just have rules:
if the application is started with no arguments, print(and/or messagebox.show) the command line arguments and quit
if the app is started with eg /gui argument (baked into the windows shortcut) it runs as gui
if the app is started with other arguments for command line operation, proceed as per those arguments
You can vary these of course, maybe you would have:
if the application is started with no arguments, run as gui
if the app is started with eg /? argument print the command line arguments and quit
if the app is started with other arguments for command line operation, proceed as per those arguments
I make this recommendation chiefly because, to be really useful as a command line app an app probably needs to take arguments from the command line, so you can use their presence or absence to infer where the app was started from. It's not perfect, of course - a shortcut can "start from windows" but pass arguments that will make i the app think it was command line, but where I'm getting to is that generally that doesn't matter because this question is not so much about "where it was started from?" as "does the app have enough info to do its work or must it ask the user for info?"
To do this without command line parameters you would need to be able to find what process launches your one. This question shows how to find the parent process id Finding parent process ID on Windows
However, if you launch a program from command prompt it's parent process id is not the one that the command prompt has. You might have to do something that reads event log and after all that realise that what you need will require admin rights. The above suggestion to use command arguments is probably a better solution but I'd be curious to see if it can be done without them.

How to read input/output from the command prompt that has been launched by a user

I have a training and testing application that tells gives users certain tasks and then programmatically determines whether they have been performed. One of the tasks is typing something in the command line prompt "dir /s" for example.
I know I can redirect the input/output of an instance of "cmd.exe" that my own application creates but how can I do the same for an instance that the user has launched?
Also, I want the user to be able to see results of their actions in the command-prompt rather than my app silently swallowing them.
You can use the Process class to find the process.
Process.GetProcessesByName("cmd")
Which will return an array of cmd processes.
See: Process.GetProcessesByName
See Determine if current application is activated (has focus) to determine which process has focus.
After that you can redirect the output of the process to your application.

Has my application been started by hand?

I am developing a Forms application that is either supposed to start on Windows boot (by registering a key in the registry) or by the actual user.
In case the user starts it by hand (i.e. clicking the executable file), I want the form to show up. In case Windows triggered the event, I want the application to be hidden (into the tray bar).
Is there a way to programmatically determine which of those cases occured?
I would suggest adding a command line parameter, something like -minimized and pass that when starting it on boot.
See this thread if you are looking for how to read the arguments.
You can start you application upon Windows startup with a certain parameter, while a user will start it without this parameter.
The easiest way to do that, that I can think about, is:
When you register to run in Windows boot, pass a parameter (some default parameter), in case when user will run it, parameter will be absent.
In this way you would be able, depending on presence or absence of a paremeter, to determine the startup option of your program.

Execute as console application when parameter is set

a few days ago, I developed a WinForms application in C# using VC#2010 Express. After creating and saving the project, I made it a console application in the project settings, so additional to the start form, a windows command line pops up. I used the console to output debug information (simply with the Console.Write() function).
So now, my app is ready for a release. I do not want the user to see the console, for sure, but I'd like to integrate a way to show the console to interested persons. So, the best way is an argument, I think (like -console), but I dont know how to start the program as a console application only when an argument is set. I know how to handle the args[] array, for sure, but I have no idea how to show the console when there is an arg. I already tried it by simply starting the program via command line(CMD -> prog.exe), but it did not work. The program starts, but the console directly shows the current path after starting the program, as usual.
Is there any possibility for my intention?
AFAIK, there is no way to have a single binary support both console and non-console behaviour, with the choice being made on start-up. All the work-arounds I've seen use two binaries.
There's a discussion here on how Python deals with the problem (boldface mine):
The standard Python.exe that comes with Python is known as a console application (this means it has been built to interact with a Windows console, otherwise known as a DOS box or command prompt). Although you can execute your Tkinter programs using Python.exe, your program will always be associated with a Windows console. It works just fine, but has the following side effects:
If you execute Python.exe from Windows Explorer, a new empty console window is created; then the Tkinter windows are created.
If you execute a Tkinter application under Python.exe from a command prompt, the command prompt doesn't return until the Tkinter application has finished. This will be a surprise for many users, who expect that executing a GUI program returns the command prompt immediately.
To get around this problem, Python comes with a special GUI version called Pythonw.exe. This is almost identical to the standard Python.exe, except it's not a console program, so doesn't suffer the problems described previously.
You could get the command line arguments for the Winform in the forms load event and then if the argument is for the console, open the console.
private void Form1_Load(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
foreach(string arg in args)
{
if(arg == "consoleargument")
{
// Run console
}
}
}
You can link to kernel32.dll and use the function AttachConsole.
Here's an example: http://www.csharp411.com/console-output-from-winforms-application/

Running a program at startup

I am currently working on a c# wpf project. I have added code to the program so that it creates a registry key to start the program automatically at user logon and have also written the program so that it can minimise to the system tray.
When the user launches the program themselves manually I obviously want the program to appear in the middle of the screen like normal but when the program launches automatically at startup I want it to load up minimised.
Is there a way to determine if the program was launched by the user or launched at startup so that I can make it load minimised instead of appearing on the screen at startup.
The easiest way would be to register your registry keys with a command line argument, so that when the program starts up automatically you can simply check the args in your main method.
As long as the user doesn't start it from the command line with that argument, you will be able to determine whether the program was auto-started or the icon was clicked.

Categories