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,