添加开机自启动
This commit is contained in:
92
App.xaml.cs
92
App.xaml.cs
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Win32;
|
||||
using GeekDesk.Constant;
|
||||
using Microsoft.Win32;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
@@ -23,96 +24,17 @@ namespace GeekDesk
|
||||
{
|
||||
|
||||
bool ret;
|
||||
mutex = new System.Threading.Mutex(true, "GeekDesk", out ret);
|
||||
mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out ret);
|
||||
if (!ret)
|
||||
{
|
||||
Environment.Exit(0);
|
||||
}
|
||||
#region 设置程序开机自动运行(+注册表项)
|
||||
try
|
||||
{
|
||||
//SetSelfStarting(true, "GeekDesk.exe");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region 注册表开机自启动
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 开机自动启动
|
||||
/// </summary>
|
||||
/// <param name="started">设置开机启动,或取消开机启动</param>
|
||||
/// <param name="exeName">注册表中的名称</param>
|
||||
/// <returns>开启或停用是否成功</returns>
|
||||
public bool SetSelfStarting(bool started, string exeName)
|
||||
{
|
||||
RegistryKey key = null;
|
||||
try
|
||||
{
|
||||
string exeDir = System.Windows.Forms.Application.ExecutablePath;
|
||||
//RegistryKey HKLM = Registry.CurrentUser;
|
||||
//key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
|
||||
key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
|
||||
|
||||
if (key == null)//如果该项不存在的话,则创建该子项
|
||||
{
|
||||
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
|
||||
}
|
||||
if (started)
|
||||
{
|
||||
try
|
||||
{
|
||||
object ob = key.GetValue(exeName, -1);
|
||||
|
||||
if (!ob.ToString().Equals(exeDir))
|
||||
{
|
||||
if (!ob.ToString().Equals("-1"))
|
||||
{
|
||||
key.DeleteValue(exeName);//取消开机启动
|
||||
}
|
||||
key.SetValue(exeName, exeDir);//设置为开机启动
|
||||
}
|
||||
key.Close();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
key.DeleteValue(exeName);//取消开机启动
|
||||
key.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (key != null)
|
||||
{
|
||||
key.Close();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// private void WriteLog(object exception)
|
||||
// {
|
||||
// Exception ex = exception as Exception;
|
||||
|
||||
Reference in New Issue
Block a user