Click here to Skip to main content
Click here to Skip to main content

Run C# application on user logon (Windows Forms)

By , 13 Nov 2012
 

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)

About the Author

ProgramFOX

Belgium Belgium
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThat's wrong!memberBernhard Hiller12-Nov-12 21:58 
AnswerRe: That's wrong!memberProgramFOX13-Nov-12 6:20 
GeneralMy vote of 5member Gun Gun Febrianza24-Oct-12 5:38 
QuestionHow to do this at installation timememberazizullah11-Oct-12 5:56 
AnswerRe: How to do this at installation timememberProgramFOX12-Oct-12 5:19 
You don't need to use my download source code.
You can change the Form class into the Form
that you need. So, you can invoke the shortcut creation method automatically.
If you only want to create the shortcut if the shortcut isn't existing, use this code:
if (!System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "MyShortcut.lnk"))
{
// create shortcut
}

AnswerRe: How to do this at installation timememberzf.liu16-Oct-12 20:37 
Questionhimemberzelinaa11-Oct-12 4:39 
GeneralMy vote of 5memberDrABELL10-Oct-12 9:01 
GeneralRe: My vote of 5memberProgramFOX12-Oct-12 5:23 
GeneralRe: My vote of 5memberDrABELL12-Oct-12 10:28 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 13 Nov 2012
Article Copyright 2012 by ProgramFOX
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid