Winform Automatic Execution program C # automatically runs upon startup

Source: Internet
Author: User

Winform automatically runs the program C # when it is started. First, we should make preparations. Of course, we will introduce the namespace.

Using Microsoft. Win32;
Using system. IO; then let's start and prepare the location of our instance. There are two ways to get the running directory,

First, splicing

// Obtain the current path of the file
String dir = directory. getcurrentdirectory ();
// Obtain all the paths of the executable file
String exedir = dir + @ "\ setstart.exe"; hey, you can do the same.

String strname = application. executablepath; of course, we are going to operate the Registry. Hey hey, use the 360 guard to test it to prevent me from restarting the computer. Haha, of course, you can also view the following input msconfig

Hey. Okay. Now we get our file.

Declare a field that can use the registry.

Registrykey Reg = NULL; then we open the Registry

Reg = registry. localmachine. opensubkey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true); of course, if there is no such thing, you can create it,

If (REG = NULL)
Reg = registry. localmachine. createsubkey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run"); OK, you can write the registry value.

If (isautorun)
{
Reg. setvalue (name, filename );
MessageBox. Show ("black screen is available when boot is enabled", "prompt", messageboxbuttons. OK, messageboxicon. information );
} OK, complete, encapsulate it.

Public static void setautorun (string filename, bool isautorun)
{
Registrykey Reg = NULL;
Try
{
If (! File. exists (filename ))
{
Throw new exception ("What should I do if I have this file ");
}
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 );
MessageBox. Show ("black screen is available when boot is enabled", "prompt", messageboxbuttons. OK, messageboxicon. information );
}

Else
Reg. setvalue (name, false );
}
Catch (exception ex)
{

Throw new exception (ex. tostring ());
}
Finally
{
If (Reg! = NULL)
{
Reg. Close ();
}
}

} Of course, I just implemented it,

Related Article

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.