-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatService.cs
More file actions
155 lines (138 loc) · 4.33 KB
/
BatService.cs
File metadata and controls
155 lines (138 loc) · 4.33 KB
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
using System;
using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
namespace Bat4WindowsService
{
/// <summary>
/// @author: liusx
/// @repository: https://github.com/l634666/Bat4WindowsService
/// </summary>
partial class BatService : ServiceBase
{
/// <summary>
/// 日志文件
/// </summary>
private static string _LogAddress;
public BatService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteLog("服务启动!");
try
{
silentRunBat(ConfigInfo.StartupBatFilePath);
}
catch (System.Exception e)
{
WriteExceptionLog(e);
}
}
protected override void OnStop()
{
WriteLog("服务停止!");
try
{
silentRunBat(ConfigInfo.ShutdownBatFilePath);
}
catch (System.Exception e)
{
WriteExceptionLog(e);
}
}
/// <summary>
/// 静默执行 bat
/// </summary>
/// <param name="fullBatFilePath"></param>
protected void silentRunBat(String fullBatFilePath)
{
Process p = new Process();
if (Path.IsPathRooted(fullBatFilePath))
{
p.StartInfo.FileName = fullBatFilePath;
}
else
{
p.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fullBatFilePath);
}
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(p.StartInfo.FileName);
p.StartInfo.CreateNoWindow = true; //不创建该进程的窗口
p.StartInfo.UseShellExecute = false; //不使用shell壳运行
p.Start();
}
/// <summary>
/// 打印到LOG文件
/// </summary>
/// <param name="msg">日志内容</param>
public static void WriteLog(String content)
{
try
{
StreamWriter sw = null;
FileInfo file = new FileInfo(LogFilePath());
if (!file.Exists)
{
sw = file.CreateText();
}
else {
sw = file.AppendText();
}
//把信息输出到文件
sw.WriteLine("当前时间:" + DateTime.Now.ToString());
sw.WriteLine("提示信息:" + content);
sw.WriteLine();
sw.Close();
}
catch (System.Exception e) { }
}
/// <summary>
/// 将异常打印到LOG文件
/// </summary>
/// <param name="ex">异常</param>
public static void WriteExceptionLog(Exception ex)
{
try
{
StreamWriter sw = null;
FileInfo file = new FileInfo(LogFilePath());
if (!file.Exists)
{
sw = file.CreateText();
}
else
{
sw = file.AppendText();
}
//把异常信息输出到文件
sw.WriteLine("当前时间:" + DateTime.Now.ToString());
sw.WriteLine("异常信息:" + ex.Message);
sw.WriteLine("异常对象:" + ex.Source);
sw.WriteLine("调用堆栈:\n" + ex.StackTrace.Trim());
sw.WriteLine("触发方法:" + ex.TargetSite);
sw.WriteLine();
sw.Close();
}
catch (System.Exception e) { }
}
/// <summary>
/// 日志文件路径
/// </summary>
/// <returns></returns>
private static string LogFilePath() {
if (string.IsNullOrEmpty(_LogAddress))
{
if (Path.IsPathRooted(ConfigInfo.LogFilePath))
{
_LogAddress = ConfigInfo.LogFilePath;
}
else
{
_LogAddress = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigInfo.LogFilePath);
}
}
return _LogAddress;
}
}
}