写个winform的程序,因为类似网管所以需要设置开机自启,网上好多的都不能用。终于搜到一个,修改以后,增加上命名空间能用了。
现在补齐了,分享给大家。
Auto.cs
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;
public static void SetAutoStart()
{
try
{
string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
string path = Application.ExecutablePath; //将当前程序起动路径
string name = Path.GetFileName(path); //获得应用程序名称
var regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath, true);
if (regKey == null) regKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(regPath);
regKey.SetValue(name, path);
}
catch
{
}
}
我把它写成一个(Auto.cs)类,方法改成了public和static,在自己的程序中,直接类调用就行了。
Auto.SetAutoStart();