try { string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (rgkRun == null) { rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); } rgkRun.SetValue("dhstest", path); // Please set your name by yourself. } catch { Debug.Log(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); } finally { regeditkey(); }
Using the above code to write the registry, you can complete the self-boot. But when Unity executes, it will report an error because it does not have permission to access registry information. Solution: When Unity is started, right-click to open it with administrator privileges, and then the heart will find that the error disappears. But at this time, we did not set the program we want to publish as boot-up, but set Unity as boot-up, so after publishing, we perform the above-mentioned operation. Yes.
Amend the Registry:
try { string path = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (rgkRun == null) { rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); } rgkRun.DeleteValue("dhstest", false); } catch { Debug.Log("error"); } finally { regeditkey(); }
The regeditkey method:
private void regeditkey() { RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if (rgkRun.GetValue("dhstest") == null) { Debug.Log( "Self-startup to shutdown"); } else { Debug.Log("Start to Open"); } }