Click here to Skip to main content
15,878,959 members
Articles / Programming Languages / C#
Tip/Trick

Run C# application on user logon (Windows Forms)

Rate me:
Please Sign up or sign in to vote.
4.98/5 (18 votes)
27 Oct 2014CPOL1 min read 78.8K   3K   66   10
A tip about how to run a Windows Forms application in C# on user logon

Introduction 

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:

C#
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:

Image 1

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

Image 2

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

C#
using IWshRuntimeLibrary;

Then, create the shortcut: 

C#
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:

C#
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.

License

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


Written By
Student
Europe Europe
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThat's wrong! Pin
Bernhard Hiller12-Nov-12 21:58
Bernhard Hiller12-Nov-12 21:58 
AnswerRe: That's wrong! Pin
Thomas Daniels13-Nov-12 6:20
mentorThomas Daniels13-Nov-12 6:20 
GeneralMy vote of 5 Pin
Gun Gun Febrianza24-Oct-12 5:38
Gun Gun Febrianza24-Oct-12 5:38 
QuestionHow to do this at installation time Pin
azizullah11-Oct-12 5:56
azizullah11-Oct-12 5:56 
AnswerRe: How to do this at installation time Pin
Thomas Daniels12-Oct-12 5:19
mentorThomas Daniels12-Oct-12 5:19 
AnswerRe: How to do this at installation time Pin
zf.liu16-Oct-12 20:37
zf.liu16-Oct-12 20:37 
Questionhi Pin
zelinaa11-Oct-12 4:39
zelinaa11-Oct-12 4:39 
GeneralMy vote of 5 Pin
DrABELL10-Oct-12 9:01
DrABELL10-Oct-12 9:01 
GeneralRe: My vote of 5 Pin
Thomas Daniels12-Oct-12 5:23
mentorThomas Daniels12-Oct-12 5:23 
GeneralRe: My vote of 5 Pin
DrABELL12-Oct-12 10:28
DrABELL12-Oct-12 10:28 

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

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