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); // 名字请自行设置
}
catch
{
Debug.Log(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
}
finally
{
regeditkey();
}
用上述代码写注册表,就可以完成自启。但是在unity执行时会报错,原因是不具备权限访问注册表信息,解决方法:在启动Unity时右击选择以管理员权限打开,之后之心就会发现这个错误消失了,但此时我们并未将我们要发布的程序设置为自启,而是将Unity设置为开机自启,所以在发布之后执行上述操作即可。