資源簡介
在c#創建的開機自啟動服務里,調用外部可執行文件有以下問題:
1、帶窗口的交互式的exe文件調用后,實際并沒有被執行;
2、服務是隨windows啟動的,服務啟動后可能windows桌面還沒出來,會報錯誤,導致程序無法執行;
3、安裝服務需管理員權限
等問題。
本例針對上面的一些問題進行解決:
1、調用帶窗口的交互式的exe文件,參見Interop.cs文件,
在服務調用問文件WindowsService.cs里面這樣引用
Interop.CreateProcess(@"d:\getp.exe", @"C:\Windows\System32\"); //執行
這里的exe可以是任意的。
2、在服務里建了個線程,延時執行exe文件,避免了第2個問題,同時循環執行,很多軟件的服務不斷彈出新聞廣告就這這樣子。
3、管理員權限問題:
在項目上點右鍵選“屬性”,選擇“安全性”,勾選復選框“啟用ClickOnce”
在Properties里面就會出現“app.manifest”文件,對其進行修改
改成
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
本例安裝進程名為“我的數據服務”,每隔200秒執行getp.exe文件。
運行時可選擇“1”進行安裝,“3”進行卸載,安裝完畢后在服務里可以看到“我的數據服務”項目。
1、帶窗口的交互式的exe文件調用后,實際并沒有被執行;
2、服務是隨windows啟動的,服務啟動后可能windows桌面還沒出來,會報錯誤,導致程序無法執行;
3、安裝服務需管理員權限
等問題。
本例針對上面的一些問題進行解決:
1、調用帶窗口的交互式的exe文件,參見Interop.cs文件,
在服務調用問文件WindowsService.cs里面這樣引用
Interop.CreateProcess(@"d:\getp.exe", @"C:\Windows\System32\"); //執行
這里的exe可以是任意的。
2、在服務里建了個線程,延時執行exe文件,避免了第2個問題,同時循環執行,很多軟件的服務不斷彈出新聞廣告就這這樣子。
3、管理員權限問題:
在項目上點右鍵選“屬性”,選擇“安全性”,勾選復選框“啟用ClickOnce”
在Properties里面就會出現“app.manifest”文件,對其進行修改
改成
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
這樣就可以管理員權限安裝了。
編譯執行后:
本例安裝進程名為“我的數據服務”,每隔200秒執行getp.exe文件。
運行時可選擇“1”進行安裝,“3”進行卸載,安裝完畢后在服務里可以看到“我的數據服務”項目。
上一篇http://www.haolizi.net/example/view_10836.html
服務只是一般的建立服務,調用外部exe是不可用的。這里做個專門例子進行說明。
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Runtime.InteropServices;
using?System.Security.Principal;
using?System.Text;
using?System.Threading.Tasks;
namespace?ConsoleWithWindowsService
{
????class?Interop
????{
????????public?static?void?CreateProcess(string?app?string?path)
????????{
????????????bool?result;
????????????IntPtr?hToken?=?WindowsIdentity.GetCurrent().Token;
????????????IntPtr?hDupedToken?=?IntPtr.Zero;
????????????PROCESS_INFORMATION?pi?=?new?PROCESS_INFORMATION();
????????????SECURITY_ATTRIBUTES?sa?=?new?SECURITY_ATTRIBUTES();
????????????sa.Length?=?Marshal.SizeOf(sa);
????????????STARTUPINFO?si?=?new?STARTUPINFO();
????????????si.cb?=?Marshal.SizeOf(si);
????????????int?dwSessionID?=?WTSGetActiveConsoleSe
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????49152??2017-03-16?12:53??創建服務\.vs\ConsoleWithWindowsService\v14\.suo
?????文件????????189??2016-09-07?09:27??創建服務\ConsoleWithWindowsService\App.config
?????文件??????14848??2017-03-16?12:20??創建服務\ConsoleWithWindowsService\bin\Debug\ConsoleWithWindowsService.exe
?????文件????????189??2016-09-07?09:27??創建服務\ConsoleWithWindowsService\bin\Debug\ConsoleWithWindowsService.exe.config
?????文件??????26112??2017-03-16?12:20??創建服務\ConsoleWithWindowsService\bin\Debug\ConsoleWithWindowsService.pdb
?????文件??????22696??2017-03-16?12:15??創建服務\ConsoleWithWindowsService\bin\Debug\ConsoleWithWindowsService.vshost.exe
?????文件????????189??2016-09-07?09:27??創建服務\ConsoleWithWindowsService\bin\Debug\ConsoleWithWindowsService.vshost.exe.config
?????文件???????3301??2017-03-14?20:09??創建服務\ConsoleWithWindowsService\bin\Debug\ConsoleWithWindowsService.vshost.exe.manifest
?????文件???????3500??2017-03-16?10:13??創建服務\ConsoleWithWindowsService\ConsoleWithWindowsService.csproj
?????文件???????6863??2017-03-16?09:12??創建服務\ConsoleWithWindowsService\Interop.cs
?????文件???????4138??2017-03-16?10:44??創建服務\ConsoleWithWindowsService\Program.cs
?????文件???????3301??2017-03-16?12:48??創建服務\ConsoleWithWindowsService\Properties\app.manifest
?????文件???????1358??2016-09-07?09:27??創建服務\ConsoleWithWindowsService\Properties\AssemblyInfo.cs
?????文件???????3779??2017-03-16?10:14??創建服務\ConsoleWithWindowsService\ServiceHelper.cs
?????文件???????2000??2017-03-16?12:19??創建服務\ConsoleWithWindowsService\WindowsService.cs
?????文件???????1118??2016-09-07?09:29??創建服務\ConsoleWithWindowsService\WindowsService.Designer.cs
?????文件???????1042??2016-09-07?09:28??創建服務\ConsoleWithWindowsService.sln
?????目錄??????????0??2017-03-16?10:07??創建服務\.vs\ConsoleWithWindowsService\v14
?????目錄??????????0??2017-03-16?12:20??創建服務\ConsoleWithWindowsService\bin\Debug
?????目錄??????????0??2016-09-07?10:36??創建服務\ConsoleWithWindowsService\bin\Release
?????目錄??????????0??2017-03-16?12:56??創建服務\ConsoleWithWindowsService\obj\Debug
?????目錄??????????0??2017-03-16?10:07??創建服務\.vs\ConsoleWithWindowsService
?????目錄??????????0??2017-03-16?10:06??創建服務\ConsoleWithWindowsService\bin
?????目錄??????????0??2017-03-16?10:06??創建服務\ConsoleWithWindowsService\obj
?????目錄??????????0??2017-03-16?12:48??創建服務\ConsoleWithWindowsService\Properties
????...D.H.?????????0??2017-03-16?10:07??創建服務\.vs
?????目錄??????????0??2017-03-16?12:19??創建服務\ConsoleWithWindowsService
?????目錄??????????0??2017-03-16?10:07??創建服務
-----------?---------??----------?-----??----
???????????????143775????????????????????28
............此處省略1個文件信息
- 上一篇:串口測試和發送數據(波特率)
- 下一篇:C# 程序捆綁釋放運行
評論
共有 條評論