views:

3275

answers:

3

Hello all,

I have my c# program set to run when windows starts by adding it to the Windows Registry. It works fine, but here is my problem...

I want the program to start in the system tray, but only when it is started automatically by Windows. If the user double-clicks the program from the desktop, then I do not want the program to start in the system tray. Right now, I can either have it always open in normal window mode or always open in the system tray.

Is there anyway to maybe determine if my program was launched by Windows startup? Or is there an alternative to this problem?

Thanks!

+5  A: 

Add an argument when started by the registry. You can definitely do this with a batch script, or maybe in the registry directly.

You will see the parameter in the argument to your static main function, and can act accordingly.

Nick
+5  A: 

I would suggest using command line arguments. If the user double clicks on an icon, then the program should start with something like this: myProgram.exe /i and if it starts with Windows, then it should start something like myProgram.exe /w.

You can parse command line arguments in the main of your program. See here:

http://www.c-sharpcorner.com/UploadFile/mahesh/CmdLineArgs03212006232449PM/CmdLineArgs.aspx

BFree
So, my only 2 options are by user or windows, so I can hide my form if not by user. Now, how do I pass a string, say "user" into a exe file for when it is double clicked...
All that can be passed in as command line args. For example, if this were the command line arg: /u="foo" /i="bar", then the args array passed into main would contain two elements. [0] = "/u=foo", [1] = "/i=bar". You can write your own simple parsing methods to parse out what you need.
BFree
Thanks for all your help, I appreciate it...I understand how to parse the arguments in my code. Now I just don't understand how the arguments get passed into the code. This is a consumer program, I would like them to just double click the icon, and it work...not msss with the command line
I would suggest specifying the 'start parameters' of the windows service to indicate that it is being started as a service. When it is started via a shortcut, just don't pass the parameters, then you know its being launched by a user. Not perfect, but should get the job done.
TJB
A: 

"I have my c# program set to run when windows starts by adding it to the Windows Registry. It works fine, but here is my problem..."

i want my c# application to start when windows starts.. how do i do this ?? what entries should be made in registry ??