I have various ways to start running
This section describes a common
Use the configuration file to set whether to boot
1,
Set a function to check whether a auto-START key exists.
Private bool isvalueexist (string keyValue)
{
Registrykey HKLM = registry. localmachine;
Registrykey run = HKLM. createsubkey (@ "software/Microsoft/Windows/CurrentVersion/Run ");
String valuestr1 = NULL;
Try
{
Valuestr1 = (string) Run. getvalue (keyValue );
HKLM. Close ();
}
Catch (exception er1)
{
MessageBox. Show (er1.message. tostring (), "prompt", messageboxbuttons. OK, messageboxicon. Error );
}
If (valuestr1 = NULL)
Return false;
Else
Return true;
}
2,
Setautovalue ()
Private void setautovalue (string keyValue)
{
// Check whether the key value already exists and will not be registered again
Bool RT = This. isvalueexist (keyValue );
If (RT)
{
// MessageBox. Show (keyValue + "already exists, no longer created ");
Return;
}
Registrykey HKLM = registry. localmachine;
Registrykey run = HKLM. createsubkey (@ "software/Microsoft/Windows/CurrentVersion/Run ");
Try
{
Run. setvalue (keyValue, application. executablepath); // obtain the path and file name
// MessageBox. Show ("it has been set to auto-start !! "," Prompt ", messageboxbuttons. OK, messageboxicon. information );
HKLM. Close ();
}
Catch (exception my)
{
MessageBox. Show (My. Message. tostring (), "prompt", messageboxbuttons. OK, messageboxicon. Error );
}
}
3,
Delautovalue ()
Private void delautovalue (string keyValue)
{
// Check whether the key value already exists. If it does not exist, no operation is performed.
Bool RT = This. isvalueexist (keyValue );
If (! RT)
{
// MessageBox. Show (keyValue + "does not exist, no operation required ");
Return;
}
Registrykey HKLM = registry. localmachine;
Registrykey run = HKLM. createsubkey (@ "software/Microsoft/Windows/CurrentVersion/Run ");
Try
{
// Run. setvalue (keyValue, application. executablepath); // obtain the path and file name
Run. deletevalue (keyValue );
// MessageBox. Show ("auto start has been successfully canceled !! "," Prompt ", messageboxbuttons. OK, messageboxicon. information );
HKLM. Close ();
}
Catch (exception my)
{
MessageBox. Show (My. Message. tostring (), "prompt", messageboxbuttons. OK, messageboxicon. Error );
}
}
4,
Main function autostartswitch ()
Private void autostartswitch (string keynamevalue, bool istart)
{
If (istart)
This. setautovalue (keynamevalue );
Else
This. delautovalue (keynamevalue );
Return;
}
String auto = "Auto ";
String autovalue = "";
5,
Whether to enable automaticoperation ()
Private void automaticoperation ()
{
Autovalue = getconfigvalue ("setautovalue ");
This. isvalueexist ("self-start running ");
Try
{
If (Auto = autovalue)
{
Autostartswitch ("self-starting", true );
}
If (Auto! = Autovalue)
{
Autostartswitch ("self-starting", false );
}
}
Catch (exception)
{
Return;
}
}
6,
The following is the configuration file touchscreenbrowser. xml.
<? XML version = "1.0" encoding = "UTF-8"?>
<Touchbrowser>
<Setautovalue> auto </setautovalue>
</Touchbrowser>
7,
Getconfigvalue ()
// Obtain the configuration file value
Protected string getconfigvalue (string configname)
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (application. startuppath + "/touchscreenbrowser. xml ");
Xmlnodelist nodelist = xmldoc. selectsinglenode ("touchbrowser"). childnodes; // retrieve all child nodes of the touchbrowser Node
Foreach (xmlnode Xn in nodelist) // traverses all subnodes
{
If (Xn. Name = configname)
{
Return XN. innertext;
}
}
Return "";
}
Source code download: http://download.csdn.net/source/645945