Home > Backend Development > C#.Net Tutorial > Detailed explanation of C# startup program without logging into the computer (pictures and text)

Detailed explanation of C# startup program without logging into the computer (pictures and text)

黄舟
Release: 2017-03-28 11:39:12
Original
1528 people have browsed it

本文主要介绍了创建系统服务;开启服务,启动程序。具有一定的参考价值,下面跟着小编一起来看下吧

阅读目录

  • 创建系统服务

  • 开启服务,启动程序

我们知道开机自启动程序如果在用户不登录的情况下是不启动的,但是服务类程序是可以跨过用户登录启动的,例如IIS服务,SQL服务。如果我们已经写好了桌面应用程序,又希望他开机自启动,那就需要借助系统服务在未登录的时候打开程序。

创建系统服务

在VS中创建Windows服务:

在Service的OnStart方法中,启动程序,代码如下:

 protected override void OnStart(string[] args)
 {
  if (!IsExistProcess("程序名"))
  {
   //程序路径
   string exePath = "";
   Process.Start(exePath);
  }
  string path = AppDomain.CurrentDomain.BaseDirectory;
  FileInfo fi = new FileInfo(path + "\\info.txt");
  using (FileStream stream = fi.OpenWrite())
  {
   StreamWriter streamWriter = new StreamWriter(stream);
   streamWriter.Write("服务启动日期:" + DateTime.Now.ToString());
   streamWriter.Flush();
   streamWriter.Close();
  }
 }
 protected override void OnStop()
 {
  string path = AppDomain.CurrentDomain.BaseDirectory;
  FileInfo fi = new FileInfo(path + "\\info.txt");
  using (FileStream stream = fi.OpenWrite())
  {
   StreamWriter streamWriter = new StreamWriter(stream);
   streamWriter.Write("服务关闭日期:" + DateTime.Now.ToString());
   streamWriter.Flush();
   streamWriter.Close();
  }
 }
 /// <summary>
 /// 判断进程是否开启
 /// </summary>
 /// <param name="processName"></param>
 /// <returns></returns>
 private bool IsExistProcess(string processName)
 {
  Process[] MyProcesses = Process.GetProcesses();
  foreach (Process MyProcess in MyProcesses)
  {
   if (MyProcess.ProcessName.CompareTo(processName) == 0)
   { return true; }
  }
  return false;
 }
Copy after login

在Service的设计视图添加安装程序:

设置ProcessInstaller的Account为LocalSystem

设置serviceInstaller的StartType为Automatic,ServiceName和Description为进程中显示的名字和描述

回到目录

开启服务,启动程序

程序编译好,取出BIN文件夹,添加开启服务和关闭服务的批处理文件,如下图:

开启桌面程序的关键点,更改进程登录模式

安装后启动.bat内容为:

安装服务
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil AutoStart.exe
停止服务
sc stop AutoStartHik
更改登录
sc config AutoStartHik type= interact type= own
启动服务
sc start AutoStartHik
Copy after login

卸载服务.bat内容为:

段落引sc stop AutoStartHik
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil AutoStart.exe /u
Copy after login

至此执行启动服务的bat文件后完成,开机自启动桌面程序。

注:有朋友在问题中提到了运行的问题,我在这里截张图

运行后的程序如果有和桌面交互的语句,会有以下提示,如你MessgeBox语句

点进去之后会有一个全新的桌面,上面运行着你通过服务启动的程序,如果和桌面不交互,你在进程里可以看到exe正在运行,他的显示界面在交互式服务里自己安静的运行。

The above is the detailed content of Detailed explanation of C# startup program without logging into the computer (pictures and text). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template