开机启动 windows 服务

 

1、cmd指令,只支持服务软件,一般的控制台运行不起来


sc create ceshi binpath= D:\ceshi\ceshi.exe type= own start= auto displayname= ceshi

 

2、c# 新建windows服务项目

 https://www.1633.com/article/67350.html  基于C#实现Windows服务

3、NSSM工具

nssm可以替代 sc 指令,sc指令不能把普通控制台作为服务。

nssm的服务本质是把nssm作为服务,传参就是控制台程序。

http://www.nssm.cc/

C:\Program Files\dotnet

nssm.exe放到dotnet的文件夹,这个文件有自动配置环境变量。

可以在cmd直接运行nssm指令

nssm install xxxx

 

4、Topshelf 搭建 Windows 服务 C#类库

 测试 win7失败       注意杀毒软件

5、winform开机启动

string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("HTSDServices", path);
rk2.Close();
rk.Close();

 

6、winsw   注册成服务

 

7、开始菜单用快捷方式运行登录启动

1
2
3
AutoStart a = new AutoStart();
a.ShortCutDelete(System.Windows.Forms.Application.ExecutablePath, Environment.GetFolderPath(Environment.SpecialFolder.Startup));
a.ShortCutCreate("Daemon_AOI上传数据程序", System.Windows.Forms.Application.ExecutablePath, Environment.GetFolderPath(Environment.SpecialFolder.Startup));

  加域系统,每一个账号都要点一下生成开始菜单的启动快捷方式。每个账号登录进去,自动启动。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using IWshRuntimeLibrary;
 
//https://blog.csdn.net/capsclock/article/details/128373102
namespace Daemon_AOI
{
    internal class AutoStart
    {
        /// <summary>
        /// 快捷方式的显示名称
        /// </summary>
        private string _ShortCutName;
 
        /// <summary>
        /// 程序的完整路径
        /// </summary>
        private string _AppPath;
 
        /// <summary>
        /// 操作系统的自启动目录
        /// C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
        /// C:\Users\[用户名]\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
        /// </summary>
        private string _SysAutoStartPath;
 
        /// <summary>
        /// 操作系统桌面的路径
        /// </summary>
        private string _SysDesktopPath;
 
        public string ShortCutName { set => _ShortCutName = value; get => _ShortCutName; }
 
        public string AppPath { get => _AppPath; }
 
        public string SysAutoStartPath { get => _SysAutoStartPath; }
 
        public string SysDesktopPath { get => _SysDesktopPath; }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="shortCutName">快捷方式的显示名称</param>
        public AutoStart(string shortCutName = null)
        {
            _ShortCutName = shortCutName;
            _AppPath = Process.GetCurrentProcess().MainModule.FileName;
            _SysAutoStartPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
            _SysDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        }
 
        /// <summary>
        /// 获取指定文件夹下的所有快捷方式(不包括子文件夹)
        /// </summary>
        /// <param target="">目标文件夹(绝对路径)</param>
        /// <returns></returns>
        public List<string> GetDirectoryFileList(string target)
        {
            List<string> list = new List<string>();
            list.Clear();
            string[] files = Directory.GetFiles(target, "*.lnk");
            if (files == null || files.Length == 0)
            {
                return list;
            }
            for (int i = 0; i < files.Length; i++)
            {
                list.Add(files[i]);
            }
            return list;
        }
 
        /// <summary>
        /// 获取快捷方式中的目标(可执行文件的绝对路径)
        /// </summary>
        /// <param name="shortCutPath">快捷方式的绝对路径</param>
        /// <returns></returns>
        public string GetAppPathViaShortCut(string shortCutPath)
        {
            try
            {
                WshShell shell = new WshShell();
                IWshShortcut shortct = (IWshShortcut)shell.CreateShortcut(shortCutPath);
                //快捷方式文件指向的路径.Text = 当前快捷方式文件IWshShortcut类.TargetPath;
                //快捷方式文件指向的目标目录.Text = 当前快捷方式文件IWshShortcut类.WorkingDirectory;
                return shortct.TargetPath;
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        /// 判断快捷方式是否存在
        /// </summary>
        /// <param name="path">快捷方式目标(可执行文件的绝对路径)</param>
        /// <param name="target">快捷方式所在目录(绝对路径)</param>
        /// <returns></returns>
        public bool ShortCutExist(string path, string target)
        {
            bool Result = false;
            List<string> list = GetDirectoryFileList(target);
            foreach (var item in list)
            {
                if (path == GetAppPathViaShortCut(item))
                {
                    Result = true;
                }
            }
            return Result;
        }
 
        /// <summary>
        /// 删除快捷方式(通过快捷方式目标进行删除)
        /// </summary>
        /// <param name="path">快捷方式目标(可执行文件的绝对路径)</param>
        /// <param name="target">快捷方式所在目录(绝对路径)</param>
        /// <returns></returns>
        public bool ShortCutDelete(string path, string target)
        {
            bool Result = false;
            List<string> list = GetDirectoryFileList(target);
            foreach (var item in list)
            {
                if (path == GetAppPathViaShortCut(item))
                {
                    System.IO.File.Delete(item);
                    Result = true;
                }
            }
            return Result;
        }
 
        /// <summary>
        /// 创建快捷方式
        /// </summary
        /// <param name="name">快捷方式名称</param>
        /// <param name="path">快捷方式目标(可执行文件的绝对路径)</param>
        /// <param name="target">快捷方式所在目录(绝对路径)</param>
        /// <returns></returns>
        public bool ShortCutCreate(string name, string path, string target)
        {
            bool Result = false;
            try
            {
                // 判断快捷方式是否存在
                if (ShortCutExist(path, target))
                {
                    return false;
                }
                // 如果文件夹不存在则创建
                if (!Directory.Exists(target)) Directory.CreateDirectory(target);
                // 快捷方式的路径
                string ShortCutPath = Path.Combine(target, string.Format("{0}.lnk", name));
                // 创建快捷方式对象
                WshShell Shell = new WshShell();
                IWshShortcut ShortCut = (IWshShortcut)Shell.CreateShortcut(ShortCutPath);
                // 设置快捷方式目标路径
                ShortCut.TargetPath = path;
                // 设置快捷方式目标文件夹
                ShortCut.WorkingDirectory = Path.GetDirectoryName(path);
                // 设置运行方式,默认为常规窗口
                ShortCut.WindowStyle = 1;
                // 保存快捷方式
                ShortCut.Save();
                Result = true;
            }
            catch
            {
                Result = false;
            }
            return Result;
        }
    }
}

  

 

 

 

www.erwa.cn   二娃制作

 

posted @   二娃网  阅读(143)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
www.erwa.cn 二娃备忘
点击右上角即可分享
微信分享提示