导读: region 确认按钮private void btnOK_Click(object sender, EventArgs e){string FileName = "";RegistryKey folders;folders =...
#region 确认按钮
private void btnOK_Click(object sender, EventArgs e)
{
string FileName = "";
RegistryKey folders;
folders = OpenRegistryPath(Registry.CurrentUser, @"\software\microsoft\windows\currentversion\explorer\shell folders");
string startupPath = folders.GetValue("Startup").ToString();
#region 开机启动
//如果选择开机启动
if (cbRun.Checked)
{
//需要开机启动的软件路径 + 软件名称
FileName = Application.StartupPath + @"\UpDownLoadService.exe";
FileInfo finfo = new FileInfo(FileName);
//菜单启动项中 创建快捷方式
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(startupPath + @"\服务器传输程序.lnk");
shortcut.TargetPath = FileName;
shortcut.Save();
}
else
{
FileInfo finfo = new FileInfo(startupPath + @"\服务器传输程序.lnk");
finfo.Delete();
}
#endregion
#region 打开注册表,写入信息
/// <summary>
/// 打开注册表,写入信息
/// </summary>
/// <param name="root">注册表封装节点</param>
/// <param name="s">节点信息</param>
/// <returns></returns>
private RegistryKey OpenRegistryPath(RegistryKey root, string s)
{
s = s.Remove(0, 1) + @"\";
while (s.IndexOf(@"\") != -1)
{
root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"\")));
s = s.Remove(0, s.IndexOf(@"\") + 1);
}
return root;
} #endregion
注释:此功能需要添加 Interop.IWshRuntimeLibrary.dll 动态库。