Click here to Skip to main content
11,631,706 members (72,424 online)
Click here to Skip to main content

Run C# application on user logon (Windows Forms)

, 27 Oct 2014 CPOL 38.6K 2.1K 61
Rate this:
Please Sign up or sign in to vote.
A tip about how to run a Windows Forms application in C# on user logon


Sometimes it is necessary to run a program on user logon. In this tip, I will learn 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.  Use a shortcut name such as "Clock.lnk" if you've a clock application, or "TcpServer.lnk" if you've a TCP/IP application.


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


About the Author

Belgium Belgium
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionThat's wrong! Pin
Bernhard Hiller12-Nov-12 21:58
memberBernhard Hiller12-Nov-12 21:58 
AnswerRe: That's wrong! Pin
ProgramFOX13-Nov-12 6:20
memberProgramFOX13-Nov-12 6:20 
GeneralMy vote of 5 Pin
Gun Gun Febrianza24-Oct-12 5:38
member Gun Gun Febrianza24-Oct-12 5:38 
QuestionHow to do this at installation time Pin
azizullah11-Oct-12 5:56
memberazizullah11-Oct-12 5:56 
AnswerRe: How to do this at installation time Pin
ProgramFOX12-Oct-12 5:19
memberProgramFOX12-Oct-12 5:19 
AnswerRe: How to do this at installation time Pin
zf.liu16-Oct-12 20:37
memberzf.liu16-Oct-12 20:37 
You can write Registry at HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run.
Questionhi Pin
zelinaa11-Oct-12 4:39
memberzelinaa11-Oct-12 4:39 
GeneralMy vote of 5 Pin
DrABELL10-Oct-12 9:01
memberDrABELL10-Oct-12 9:01 
GeneralRe: My vote of 5 Pin
ProgramFOX12-Oct-12 5:23
memberProgramFOX12-Oct-12 5:23 
GeneralRe: My vote of 5 Pin
DrABELL12-Oct-12 10:28
memberDrABELL12-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 | Terms of Use | Mobile
Web02 | 2.8.150723.1 | Last Updated 28 Oct 2014
Article Copyright 2012 by ProgramFOX
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid