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)

Mar 28, 2017 am 11:39 AM
c# starting program

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

阅读目录

  • 创建系统服务

  • 开启服务,启动程序

我们知道开机自启动程序如果在用户不登录的情况下是不启动的,但是服务类程序是可以跨过用户登录启动的,例如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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

See all articles