C # Set startup

Source: Internet
Author: User
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:

    1. // Add startup
    2. Registrykey ms_run = registry. localmachine. opensubkey ("software // Microsoft // windows // CurrentVersion // run", true );
    3. Ms_run.setvalue ("mistysoft", application. executablepath. tostring ());
    4. // Delete startup (set as control, registry key is still in)
    5. Registrykey ms_run = registry. localmachine. opensubkey ("software // Microsoft // windows // CurrentVersion // run", true );
    6. Ms_run.setvalue ("mistysoft ","");

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.