C # Set Automatic startup (registry mode)

Source: Internet
Author: User


. NET technology exchange group 199281001. welcome to join.

Using System; using System. collections. generic; using System. linq; using System. runtime. remoting. metadata. w3cXsd2001; using System. security. permissions; using System. text; using Microsoft. win32; // registry auxiliary class Author: BingBingpublic static class Regedit {// custom node name public static string NodeName = "XX "; // SOFTWARE node public static string SoftWare = "SOFTWARE "; /// <summary> /// read the registry value of the specified name /// </summary> /// <para M name = "key"> key of the registry value </param> // <returns> </returns> public static string GetValue (string key) {using (var aimdir = Load (SoftWare) {using (var child = aimdir. openSubKey (NodeName) {if (child = null) return null; var registData = child. getValue (key); return registData = null? Null: registData. toString ();}}} /// <summary> /// write data in the Registry /// </summary> /// <param name = "key"> key of the registry value </param>/ // <param name = "tovalue"> value </param> public static void AddValue (string key, string value) {using (var aimdir = Load (SoftWare) {using (var child = aimdir. openSubKey (NodeName, true) {if (child = null) return; if (ExistsValue (key, NodeName) return; child. setValue (key, value );}}} /// <Summary> /// Delete the registry key specified in the Registry /// </summary> /// <param name = "key"> key in the registry value </param> public static void DeleteValue (string key) {using (var aimdir = Load (SoftWare) {if (! ExistsValue (key, NodeName) return; using (var child = aimdir. OpenSubKey (NodeName, true) {if (child! = Null) {child. deleteValue (key );}}}} /// <summary> /// Load /// </summary> /// <returns> </returns> public static RegistryKey Load (string rootName) {var software = Registry. currentUser; return software. openSubKey (rootName, RegistryKeyPermissionCheck. readWriteSubTree, System. security. accessControl. registryRights. fullControl) ;}/// <summary> /// determine whether the specified registry key exists /// </summary> /// <param name = "node Name "> Name of the node on the left of the registry </param> // <returns> </returns> public static bool Exists (string nodeName) {var exit = false; using (var aimdir = Load (SoftWare) {var subkeyNames = aimdir. getSubKeyNames (); if (subkeyNames. any (keyName => keyName = nodeName) {exit = true ;}} return exit ;} /// <summary> /// determine whether the specified registry key exists /// </summary> /// <param name = "nodeName"> nodes on the left of the registry </param> /// <param name = "rootName"> parent node name </Param> // <returns> </returns> public static bool ExistsValue (string key, string rootName) {var exit = false; using (var aimdir = Load (SoftWare) {using (var child = aimdir. openSubKey (NodeName, true) {if (child = null) return exit; var subkeyNames = child. getValueNames (); if (subkeyNames. any (keyName => keyName = key) {exit = true ;}}return exit ;}/// <summary> /// create /// </summary> /// <Param name = "name"> node name on the left </param> public static void Create (string nodeName) {using (var software = Load (SoftWare )) {if (! Exists (nodeName) {software. createSubKey (nodeName );}}} /// <summary> /// start with windows /// </summary> /// <param name = "path"> absolute path of the program to be started </param> public static void AutoRun (string path) {using (var autorun = Load (SoftWare + @ "\ Microsoft \ Windows \ CurrentVersion \ Run") {autorun. setValue ("xxw.run", path );}} /// <summary> /// start with windows /// </summary> /// <param name = "path"> absolute path of the program to be started </param> public static void RemoveAutoRun () {using (var autorun = Load (SoftWare + @ "\ Microsoft \ Windows \ CurrentVersion \ Run") {if (autorun. getValue ("xxw.run") = null) return; autorun. deleteValue ("xxw.run ");}}}

 

 

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.