C#中如何重启系统并在开机后自动执行当前可执行程序
2012-10-12 10:25
429 查看
1、重启系统:
(1)利用dos命令对应的可执行程序
public static void RestartOS(out string errorDesc)
{
try
{
errorDesc = string.Empty;
//---重启系统---
string tmpFileName = "shutdown.exe";
string tmpCmdLineParameters = "-r -t 0 -f";
System.Diagnostics.Process tmpShutdownProc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo tmpStartInfo =
new System.Diagnostics.ProcessStartInfo(tmpFileName, tmpCmdLineParameters);
tmpStartInfo.UseShellExecute = false;//加上这一句,否则在重启系统时会有个对话框已关闭的错误框弹出
tmpShutdownProc.StartInfo = tmpStartInfo;
tmpShutdownProc.Start();
}
catch (Exception allE)
{
errorDesc = allE.Message;
}
}
(2)利用WMI
(3)利用API
2、开机自动自动执行当前应用程序
public static void AddCurrentExeToRegisterRunOnce(out string errorDesc)
{
try
{
errorDesc = string.Empty;
//---添加注册表---
Microsoft.Win32.RegistryKey tmpRegKey =
Microsoft.Win32.Registry.CurrentUser;
tmpRegKey = tmpRegKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\RunOnce", true);
tmpRegKey.SetValue(@"tmp", Application.ExecutablePath);
}
catch (Exception allE)
{
errorDesc = allE.Message;
}
}
注:注册表中允许开机自动执行的注册键不止这一个,之所以用这一个,是因为它具有“只在下次开机时运行一次,并且是在整个用户程序加载完成后运行指定的程序。”
感兴趣的可以试一下用这个键值在HKEY_LOCAL_MACHINE下面的同名键(即Software\Microsoft\Windows\CurrentVersion\RunOnce),实际感受他们之间的区别;
(1)利用dos命令对应的可执行程序
public static void RestartOS(out string errorDesc)
{
try
{
errorDesc = string.Empty;
//---重启系统---
string tmpFileName = "shutdown.exe";
string tmpCmdLineParameters = "-r -t 0 -f";
System.Diagnostics.Process tmpShutdownProc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo tmpStartInfo =
new System.Diagnostics.ProcessStartInfo(tmpFileName, tmpCmdLineParameters);
tmpStartInfo.UseShellExecute = false;//加上这一句,否则在重启系统时会有个对话框已关闭的错误框弹出
tmpShutdownProc.StartInfo = tmpStartInfo;
tmpShutdownProc.Start();
}
catch (Exception allE)
{
errorDesc = allE.Message;
}
}
(2)利用WMI
(3)利用API
2、开机自动自动执行当前应用程序
public static void AddCurrentExeToRegisterRunOnce(out string errorDesc)
{
try
{
errorDesc = string.Empty;
//---添加注册表---
Microsoft.Win32.RegistryKey tmpRegKey =
Microsoft.Win32.Registry.CurrentUser;
tmpRegKey = tmpRegKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\RunOnce", true);
tmpRegKey.SetValue(@"tmp", Application.ExecutablePath);
}
catch (Exception allE)
{
errorDesc = allE.Message;
}
}
注:注册表中允许开机自动执行的注册键不止这一个,之所以用这一个,是因为它具有“只在下次开机时运行一次,并且是在整个用户程序加载完成后运行指定的程序。”
感兴趣的可以试一下用这个键值在HKEY_LOCAL_MACHINE下面的同名键(即Software\Microsoft\Windows\CurrentVersion\RunOnce),实际感受他们之间的区别;
相关文章推荐
- 两个小知识:C#如何设置开机启动时自动执行程序|C# WinForm打开超链接
- C#WinForm程序异常退出的捕获、继续执行与自动重启
- C#WinForm程序异常退出的捕获、继续执行与自动重启
- 如何顺利实现LINUX系统开机自动启动指定的脚本程序
- 开机自动运行自己开发的可执行程序,如何不弹窗口
- 如何让Linux系统的程序在开机时自动运行
- KingDZ 变菜鸟,每日一个C#小实例之---C#开机自动执行程序
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库
- C#WinForm程序异常退出的捕获、继续执行与自动重启
- (转载整理)程序安装流程及如何实现文件在系统后自动重启(windows)
- [转]C#写的程序如何在开机的时候自动运行
- C#WinForm程序异常退出的捕获、继续执行与自动重启
- 如何让Linux系统的程序在开机时自动运行
- KingDZ 变菜鸟,每日一个C#小实例之---C#开机自动执行程序
- 怎度网 学而时习,温故知新,不亦说乎 在Linux系统如何让程序开机时自动启动(转载自https://www.cnblogs.com/zendu/p/4991065.html)
- 在Linux系统如何让程序开机时自动启动
- 在Linux系统如何让程序开机时自动启动
- 如何使Linux系统上的程序开机后自动运行
- 转 C#WinForm程序异常退出的捕获、继续执行与自动重启
- 在Linux系统如何让程序开机时自动启动