Application discovery is done by clicking a user or starting Windows in C #

I have a C # WinForm application. Currently, it is launched from the desktop shortcut. But I would like to add it to the system startup. The user can decide whether he will start at startup or not.

If it starts at system startup, I would like to minimize it on the system tray, otherwise it will be launched on the taskbar.

Is there a way to check if it starts at startup or not?

+5
source share
2 answers

Your application will not be able to detect (by itself), it was launched at startup or by the usual launch of the user. However, you can pass arguments to your application, and then correct your application correctly. Here is an example:

First run the program.cs main method. Now, by default, you do not see the passed startup arguments. However, adding the string[] args parameter to the main() method will display the command arguments. For instance,

 static class Program { public static bool LaunchedViaStartup { get; set; } /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { Program.LaunchedViaStartup = args != null && args.Any(arg => arg.Equals("startup", StringComparison.CurrentCultureIgnoreCase)); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } 

Now the code is simple, we set a static variable in the Program class called LaunchedViaStartup , and then before starting our main form, we check to see if the command argument contains our special startup argument (via Linq). The name for this argument is just up to you.

Now in our main form (yes the main one) we have access to this property for the lifetime of the application.

 public partial class Form1 : Form { public Form1() { InitializeComponent(); MessageBox.Show(this, string.Format("Lanched Via Startup Arg:{0}", Program.LaunchedViaStartup)); } } 

Finally, to verify this, you can simply open the Project Properties window and set Command line arguments , as shown in the screenshot below.

enter image description here

Finally, to test the Startup argument outside of visual studio, add the start argument to your shortcut, such as the screenshot below.

enter image description here

+7
source

Send the arguments of the commend argument at startup from startup (define it in the shortcut path).

Get the arguments in the main application and make a decision based on the arguments. Now, it’s up to you how you achieve it.

Check MSDN

Here it is for winform

+2
source

All Articles