Click here to Skip to main content
Click here to Skip to main content
Go to top

Run C# application on user logon (Windows Forms)

, 13 Nov 2012
Rate this:
Please Sign up or sign in to vote.
A tip about how to run a C# application on Windows startup.

Introduction 

Sometimes it is necessary to run a program on user logon. In this tip, I will you how to run a C# program automatically in Windows Forms.

Using the code 

First, we need to know the location of the Startup folder.  To do that, we use this code:

string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

Now, we need to create the shortcut. To do that, we need to add a reference to the Windows ScriptHost Object Model. If you use Visual C#, you can see on this pictures how to add the reference:

And then, choose the Windows Script Host Object Model in the tabpage 'COM':

Now, add this using namespace statement at the top of your code file:

using IWshRuntimeLibrary;

Then, create the shortcut: 

WshShell shell = new WshShell();
string shortcutAddress = startupFolder + @"\MyStartupShortcut.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "A startup shortcut. If you delete this shortcut from your computer, LaunchOnStartup.exe will not launch on Windows Startup"; // set the description of the shortcut
shortcut.WorkingDirectory = Application.StartupPath; /* working directory */
shortcut.TargetPath = Application.ExecutablePath; /* path of the executable */
shortcut.Save(); // save the shortcut 

Optionally, you can set the arguments of the shortcut:

shortcut.Arguments = "/a /c";

Do that before you save the shortcut. Setting the arguments is not required; do it only if your program needs that.

But don't use a name such as "MyStartupShortcut.lnk", because there is a risk that other programs use that name, and that you overwrite that shortcut.  Create a shortcut name such as "Clock.lnk" if you've a clock application, or "TcpServer.lnk" if you've a TCP/IP application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ProgramFOX

Belgium Belgium
No Biography provided

Comments and Discussions

 
QuestionThat's wrong! PinmemberBernhard Hiller12-Nov-12 21:58 
AnswerRe: That's wrong! PinmemberProgramFOX13-Nov-12 6:20 
GeneralMy vote of 5 Pinmember Gun Gun Febrianza24-Oct-12 5:38 
QuestionHow to do this at installation time Pinmemberazizullah11-Oct-12 5:56 
AnswerRe: How to do this at installation time PinmemberProgramFOX12-Oct-12 5:19 
AnswerRe: How to do this at installation time Pinmemberzf.liu16-Oct-12 20:37 
Questionhi Pinmemberzelinaa11-Oct-12 4:39 
GeneralMy vote of 5 PinmemberDrABELL10-Oct-12 9:01 
GeneralRe: My vote of 5 PinmemberProgramFOX12-Oct-12 5:23 
If you use my code, you'll create a shortcut in the startup folder for the current
user only.
GeneralRe: My vote of 5 PinmemberDrABELL12-Oct-12 10:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 13 Nov 2012
Article Copyright 2012 by ProgramFOX
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid