資源簡介
csharp-Windows服務及進程監控程序,可以設定要監控的進程和服務名稱,監控到沒運行自動啟動.服務監控,進程監控

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Diagnostics;
using?System.IO;
using?System.Linq;
using?System.Text;
using?System.Threading;
using?System.Threading.Tasks;
using?log4net;
namespace?HQ.ServiceWatch
{
????public?class?ProcessWatcher
????{
????????static?readonly?ILog?_log?=?LogManager.GetLogger(“TraceLogFileAppender“);
????????//字段
????????public?string[]?_processAddress;
????????public?int?WaitSeconds;
????????///?
????????///?開始監控
????????///?
????????public?void?StartWatch()
????????{
????????????if?(this._processAddress?!=?null)
????????????{
????????????????if?(this._processAddress.Length?>?0)
????????????????{
????????????????????foreach?(string?str?in?_processAddress)
????????????????????{
????????????????????????if?(str.Trim()?!=?““)
????????????????????????{
????????????????????????????if?(File.Exists(str.Trim()))
????????????????????????????{
????????????????????????????????this.ScanProcessList(str.Trim());
????????????????????????????}
????????????????????????}
????????????????????}
????????????????}
????????????}
????????}
????????///?
????????///?掃描進程列表,判斷進程對應的全路徑是否與指定路徑一致
????????///?如果一致,說明進程已啟動
????????///?如果不一致,說明進程尚未啟動
????????///?
????????///?
????????private?void?ScanProcessList(string?address)
????????{
????????????Process[]?arrayProcess?=?Process.GetProcesses();
????????????foreach?(Process?p?in?arrayProcess)
????????????{
????????????????//System、Idle進程會拒絕訪問其全路徑
????????????????if?(p.ProcessName?!=?“System“?&&?p.ProcessName?!=?“Idle“)
????????????????{
????????????????????try
????????????????????{
????????????????????????if?(this.FormatPath(address)?==?this.FormatPath(p.MainModule.FileName.ToString()))
????????????????????????{
????????????????????????????//進程已啟動
????????????????????????????this.WatchProcess(p?address?WaitSeconds);
????????????????????????????return;
????????????????????????}
????????????????????}
????????????????????catch
????????????????????{
????????????????????????//拒絕訪問進程的全路徑
????????????????????????//_log.Info(“進程(“?+?p.Id.ToString()?+?“)(“?+?p.ProcessName.ToString()?+?“)拒絕訪問全路徑!“);
????????????????????}
????????????????}
????????????}
????????????//進程尚未啟動
????????????Process?process?=?new?Process();
????????????process.StartInfo.FileName?=?address;
????????????process.Start();
????????????this.WatchProcess(process?address?WaitSeconds);
????????}
????????///?
????????///?監聽進程
????????///?
????????///?
????????///?
????????private?void?WatchProcess(Process?process?string?address?int?waitSeconds)
????????{
????????????ProcessRestart?objProcessRestart?=?new?ProcessRestart(process?address?waitSeconds);
????????????Thread?thread?=?new?Thread(new?ThreadStart(objProcessRestart.RestartProcess));
????????????thread.Start();
????????}
??
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????605??2015-04-02?17:56??HQ.ServiceWatch\HQ.ServiceWatch\App.config
?????文件??????13312??2015-04-02?17:54??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\HQ.ServiceWatch.exe
?????文件????????605??2015-04-02?17:49??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\HQ.ServiceWatch.exe.config
?????文件??????32256??2015-04-02?17:54??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\HQ.ServiceWatch.pdb
?????文件??????24224??2015-04-02?17:55??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\HQ.ServiceWatch.vshost.exe
?????文件????????605??2015-04-02?17:49??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\HQ.ServiceWatch.vshost.exe.config
?????文件??????????0??2015-04-02?15:26??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\log\error.txt
?????文件???????2281??2015-03-13?16:26??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\log4net.config
?????文件?????301056??2015-04-02?14:32??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\log4net.dll
?????文件????1517265??2015-04-02?14:32??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\log4net.xm
?????文件???????1321??2015-04-02?17:52??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\Logs\20150402.log
?????文件?????279040??2015-04-02?14:33??HQ.ServiceWatch\HQ.ServiceWatch\bin\Debug\Topshelf.dll
?????文件??????12800??2015-04-02?17:55??HQ.ServiceWatch\HQ.ServiceWatch\bin\Release\HQ.ServiceWatch.exe
?????文件????????605??2015-04-02?17:49??HQ.ServiceWatch\HQ.ServiceWatch\bin\Release\HQ.ServiceWatch.exe.config
?????文件??????26112??2015-04-02?17:55??HQ.ServiceWatch\HQ.ServiceWatch\bin\Release\HQ.ServiceWatch.pdb
?????文件??????24224??2015-04-02?17:55??HQ.ServiceWatch\HQ.ServiceWatch\bin\Release\HQ.ServiceWatch.vshost.exe
?????文件????????605??2015-04-02?17:49??HQ.ServiceWatch\HQ.ServiceWatch\bin\Release\HQ.ServiceWatch.vshost.exe.config
?????文件????????490??2010-03-17?22:39??HQ.ServiceWatch\HQ.ServiceWatch\bin\Release\HQ.ServiceWatch.vshost.exe.manifest
?????文件?????301056??2015-04-02?14:32??HQ.ServiceWatch\HQ.ServiceWatch\bin\Release\log4net.dll
?????文件????1517265??2015-04-02?14:32??HQ.ServiceWatch\HQ.ServiceWatch\bin\Release\log4net.xm
?????文件?????279040??2015-04-02?14:33??HQ.ServiceWatch\HQ.ServiceWatch\bin\Release\Topshelf.dll
?????文件???????3159??2015-04-02?17:56??HQ.ServiceWatch\HQ.ServiceWatch\HQ.ServiceWatch.csproj
?????文件???????2281??2015-04-02?17:56??HQ.ServiceWatch\HQ.ServiceWatch\log4net.config
?????文件???????7249??2015-04-02?14:52??HQ.ServiceWatch\HQ.ServiceWatch\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????854??2015-04-02?17:55??HQ.ServiceWatch\HQ.ServiceWatch\obj\Debug\HQ.ServiceWatch.csproj.FileListAbsolute.txt
?????文件??????14319??2015-04-02?15:07??HQ.ServiceWatch\HQ.ServiceWatch\obj\Debug\HQ.ServiceWatch.csprojResolveAssemblyReference.cache
?????文件??????13312??2015-04-02?17:54??HQ.ServiceWatch\HQ.ServiceWatch\obj\Debug\HQ.ServiceWatch.exe
?????文件??????32256??2015-04-02?17:54??HQ.ServiceWatch\HQ.ServiceWatch\obj\Debug\HQ.ServiceWatch.pdb
?????文件??????????0??2015-04-02?14:14??HQ.ServiceWatch\HQ.ServiceWatch\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2015-04-02?14:14??HQ.ServiceWatch\HQ.ServiceWatch\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
............此處省略65個文件信息
- 上一篇:特征的選擇與提取 模式識別
- 下一篇:房屋租賃管理系統源碼
評論
共有 條評論