The principle is to add an item in the Registry Startup item.
Path:
SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run
Or directly: Run-> Regedit to find this path and add an item.
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. LINQ;
Using System. text;
Using System. Windows. forms;
UsingMicrosoft. Win32;
NamespaceCsharpstart
{
Public Partial ClassForm1: Form
{
PublicForm1 ()
{
Initializecomponent ();
}
Private Void Btnset_click ( Object Sender, eventargs E)
{
Setautorun ( @" D: \ csharpstart.exe " , True );
}
/// <Summary>
/// Set ApplicationProgramAutomatic startup
/// </Summary>
/// <Param name = "FILENAME"> Application file name </Param>
/// <Param name = "isautorun"> Whether to run automatically. If it is set to false, automatic operation is canceled. </Param>
/// <Exception CREF = "system. Exception"> An exception is thrown when the setting fails. </Exception>
Public Static Void Setautorun ( String Filename, Bool Isautorun)
{
Registrykey Reg = Null ;
Try
{
If (! System. Io. file. exists (filename ))
Throw New Exception ( " This file does not exist! " );
String name = filename. substring (filename. lastindexof ( @" \ " ) + 1 );
Reg = registry. localmachine. opensubkey ( @" SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run " , True );
If (REG = Null )
Reg = registry. localmachine. createsubkey ( @" SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run " );
If (Isautorun)
Reg. setvalue (name, filename );
Else
Reg. setvalue (name, False );
}
Catch (Exception ex)
{
Throw New Exception (ex. tostring ());
}
Finally
{
If (Reg! = Null )
Reg. Close ();
}
}
//It can also be written as a service, but the service is generally executed in the background without a program interface.
}
}
URL: http://greatverve.cnblogs.com/archive/2012/02/18/csharp-autorun.html
Refer:
C # The winform program is set to start at boot. When the configuration file is read or the image is loaded, if the relative path is set, problems will occur when starting the system (the direct program is OK ). This is because the absolute path is required for the Startup Program, and the relative path is not good. We can solve the absolute path problem of the file by processing the application. startuppath attribute.
C # It's easy to set startup through reading and writing the Registry. There are a lot of online methods:
/// <Summary>
/// Start item
/// </Summary>
/// <Param name = "started"> Start? </Param>
/// <Param name = "name"> Start Value Name </Param>
/// <Param name = "path"> Startup Program path </Param>
Public Void Runwhenstart (
Bool Started,
String Name,
String Path)
{
Registrykey HKLM = registry. localmachine;
Registrykey run = HKLM. createsubkey (
@" Software/Microsoft/Windows/CurrentVersion/run " );
If (Started =
True )
{
Try
{
Run. setvalue (name, PATH );
HKLM. Close ();
}
Catch // An exception occurs if you do not have the permission.
{}
}
Else
{
Try
{
Run. deletevalue (name );
HKLM. Close ();
}
Catch // An exception occurs if you do not have the permission.
{}
}
}
Or directly:
- // Add startup
- Registrykey ms_run = registry. localmachine. opensubkey ("software // Microsoft // windows // CurrentVersion // run", true );
- Ms_run.setvalue ("mistysoft", application. executablepath. tostring ());
- // Delete startup (set as control, registry key is still in)
- Registrykey ms_run = registry. localmachine. opensubkey ("software // Microsoft // windows // CurrentVersion // run", true );
- Ms_run.setvalue ("mistysoft ","");