. NET implementation: Set EXE to automatically start upon startup

Source: Internet
Author: User

Copy codeThe Code is as follows: private void btnSetOk_Click (object sender, EventArgs e)

{

RegCompStartRun (true, txtFullPath. Text. Trim ());

}

Private void btnCancel_Click (object sender, EventArgs e)

{

RegCompStartRun (false, txtFullPath. Text. Trim ());

}

Private void RegCompStartRun (bool cmd, string argPath)

{

String starupPath = argPath;

If (string. IsNullOrEmpty (argPath ))

{

// Obtain the full path of the current executable program

StarupPath = Application. ExecutablePath;

}

// Indicates the entry-level node in the Window registry. It reads the HKEY_LOCAL_MACHINE key of the Windows registry.

Microsoft. Win32.RegistryKey loca = Microsoft. Win32.Registry. LocalMachine;

Microsoft. Win32.RegistryKey run = loca. CreateSubKey (@ "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run ");

Try

{

// SetValue: name of the stored value

If (cmd)

{

Run. SetValue ("AutoStartupTestWinFormApp", starupPath); // Add to registration. The parameter is the name of the registered node (random)

LblDisplay. Text = "set successfully! ";

}

Else

{

Run. DeleteValue ("AutoStartupTestWinFormApp", false); // Delete the registered Node

LblDisplay. Text = "the settings have been canceled! ";

}

Loca. Close ();

}

Catch (Exception ee)

{

MessageBox. Show (ee. Message. ToString (), "prompt", MessageBoxButtons. OK, MessageBoxIcon. Error );

}

}

Note:

If you rename the exe file after setting, log out of the file, restart the file, and log on again. The set boot item does not start,

If you change to the original name, log off or restart again. After logon, the set exe file is automatically started.

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.