Pratik Singhal
Pratik Singhal

Reputation: 6492

How to run a function only on startup?

I have a desktop application written in C#, which I want o run at start-up.
Moreover there is a function,

    Index()

which should run only once per start-up.
What I mean is that this function should be invoked in such a way that It runs only when the application starts at upstart and not when user starts the application manually. I want to know, how can I do this? I haven't been able to think of a fruitful logic for this problem, despite many efforts. My desktop application will run on windows 7.

Upvotes: 0

Views: 2533

Answers (2)

Alex
Alex

Reputation: 23300

One possible easy attempt at a solution would be a command line parameter to which you can attach the "special" logic you need to run. When users then launch the application manually, this "magic" parameter won't be there, and the logic won't take place.

In a Console Application, all you need to do is check the args parameter of the Main() method.

To enable command-line arguments in the Main method in a Windows Forms application, you must manually modify the signature of Main in program.cs. The code generated by the Windows Forms designer creates a Main without an input parameter. You can also use Environment.CommandLine or Environment.GetCommandLineArgs to access the command-line arguments from any point in a console or Windows application.

Quoted from MSDN which contains as usual a lot of info.

Upvotes: 4

Thomas Levesque
Thomas Levesque

Reputation: 292615

There's no standard way to check that the app has been started at computer startup. A possible solution is to pass a command-line argument like /startup, and check for this parameter when the app starts.

Upvotes: 1

Related Questions