这篇文章主要介绍了C# winform程序实现开机自启动并且识别是开机启动还是双击启动的实现代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
开机启动程序,在很多场合都会用到,尤其是那种在后台运行的程序。
效果图:
using System;
using Microsoft.Win32;
namespace AutoStartRun
{
public sealed class SystemHelper
{
private SystemHelper() { }
/// <summary>
/// 设置程序开机启动
/// </summary>
/// <param name="strAppPath">应用程序exe所在文件夹</param>
/// <param name="strAppName">应用程序exe名称</param>
/// <param name="bIsAutoRun">自动运行状态</param>
public static void SetAutoRun(string strAppPath, string strAppName, bool bIsAutoRun)
{
try
{
if (string.IsNullOrWhiteSpace(strAppPath)
|| string.IsNullOrWhiteSpace(strAppName))
{
throw new Exception("应用程序路径或名称为空!");
}
RegistryKey reg = Registry.LocalMachine;
RegistryKey run = reg.CreateSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
if (bIsAutoRun)
{
run.SetValue(strAppName, strAppPath);
}
else
{
if (null != run.GetValue(strAppName))
{
run.DeleteValue(strAppName);
}
}
run.Close();
reg.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
/// <summary>
/// 判断是否开机启动
/// </summary>
/// <param name="strAppPath">应用程序路径</param>
/// <param name="strAppName">应用程序名称</param>
/// <returns></returns>
public static bool IsAutoRun(string strAppPath, string strAppName)
{
try
{
RegistryKey reg = Registry.LocalMachine;
RegistryKey software = reg.OpenSubKey(@"SOFTWARE");
RegistryKey run = reg.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
object key = run.GetValue(strAppName);
software.Close();
run.Close();
if (null == key || !strAppPath.Equals(key.ToString()))
{
return false;
}
return true;
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
}
}
}
调用方法:
/// <summary>
/// 设置程序开机自启动
/// </summary>
private void SetAutoRun()
{
string strFilePath = Application.ExecutablePath;
string strFileName = System.IO.Path.GetFileName(strFilePath);
try
{
SystemHelper.SetAutoRun(strFilePath + " -autostart", strFileName, !menuAutoRun.Checked);
menuAutoRun.Checked = !menuAutoRun.Checked;
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
设置开机启动就是如此简单。
【开机启动并运行】
那这个就不用说了,将命令行参数和开机注册表操作结合起来就可以了。
示例代码:
/// <summary>
/// 检查是否开机启动,并设置控件状态
/// </summary>
private void CheckAutoRun()
{
string strFilePath = Application.ExecutablePath;
string strFileName = System.IO.Path.GetFileName(strFilePath);
if (SystemHelper.IsAutoRun(strFilePath + " -autostart", strFileName))
{
menuAutoRun.Checked = true;
}
else
{
menuAutoRun.Checked = false;
}
}
private void AutoRun()
{
if (menuAutoRun.Checked)
{
string[] strArgs = Environment.GetCommandLineArgs();
if (strArgs.Length >= 2 && strArgs[1].Equals("-autorun"))
{
labText.Text = "我是开机自启动运行...";
}
}
}
总结
以上所述是小编给大家介绍的C# winform程序实现开机自启动并且识别是开机启动还是双击启动,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程学习网网站的支持!
沃梦达教程
本文标题为:C# winform程序实现开机自启动并且识别是开机启动还是双击启动
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
猜你喜欢
- 如何使用C# 捕获进程输出 2023-03-10
- c# 模拟线性回归的示例 2023-03-14
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- Oracle中for循环的使用方法 2023-07-04
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- Unity Shader实现模糊效果 2023-04-27
- Unity3D实现渐变颜色效果 2023-01-16
- .NET CORE DI 依赖注入 2023-09-27
- user32.dll 函数说明小结 2022-12-26