├── .gitattributes
├── .gitignore
├── AnyExeToService.sln
├── AnyExeToService
├── AdvInstallHelper.cs
├── AnyExeToService.csproj
├── App.config
├── App.xaml
├── App.xaml.cs
├── ExeServiceConfig.cs
├── MainWindow.xaml
├── MainWindow.xaml.cs
├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ ├── Settings.settings
│ └── app.manifest
├── Resources
│ ├── MaterialDesignColors.dll
│ ├── MaterialDesignThemes.Wpf.dll
│ ├── Newtonsoft.Json.dll
│ ├── ServiceApp.exe
│ ├── ServiceBridge.exe
│ ├── install.exe
│ └── log4net.dll
├── ServiceInfo.cs
├── main.ico
├── packages.config
└── preferences_management_service_128px_1172109_easyicon.net.ico
├── LICENSE
├── README.md
├── ServiceApp
├── ExeProcess.cs
├── MainService.Designer.cs
├── MainService.cs
├── Program.cs
├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ └── Resources.resx
├── Resources
│ ├── Newtonsoft.Json.dll
│ └── log4net.dll.dat
├── ServiceApp.csproj
├── ServiceConfig.cs
└── log4net.config
├── images
└── index.PNG
└── packages
├── MaterialDesignColors.1.1.2
├── .signature.p7s
├── MaterialDesignColors.1.1.2.nupkg
└── lib
│ └── net45
│ └── MaterialDesignColors.dll
├── MaterialDesignThemes.2.5.1
├── .signature.p7s
├── MaterialDesignThemes.2.5.1.nupkg
├── lib
│ └── net45
│ │ ├── MaterialDesignThemes.Wpf.XML
│ │ └── MaterialDesignThemes.Wpf.dll
└── src
│ └── net45
│ ├── Badged.cs
│ ├── BaseTheme.cs
│ ├── ButtonProgressAssist.cs
│ ├── Card.cs
│ ├── Chip.cs
│ ├── Clock.cs
│ ├── ClockChoiceMadeEventArgs.cs
│ ├── ClockItemButton.cs
│ ├── ColorZone.cs
│ ├── ColorZoneAssist.cs
│ ├── ComboBoxAssist.cs
│ ├── ComboBoxPopup.cs
│ ├── Converters
│ ├── BooleanToVisibilityConverter.cs
│ ├── BrushRoundConverter.cs
│ ├── BrushToRadialGradientBrushConverter.cs
│ ├── CalendarDateCoalesceConverter.cs
│ ├── CircularProgressBar
│ │ ├── ArcEndPointConverter.cs
│ │ ├── ArcSizeConverter.cs
│ │ ├── LargeArcConverter.cs
│ │ ├── LocalEx.cs
│ │ ├── RotateTransformCentreConverter.cs
│ │ └── StartPointConverter.cs
│ ├── ClockItemIsCheckedConverter.cs
│ ├── ClockLineConverter.cs
│ ├── DrawerOffsetConverter.cs
│ ├── EqualityToVisibilityConverter.cs
│ ├── HintProxyFabricConverter.cs
│ ├── ListViewItemContainerStyleConverter.cs
│ ├── MathConverter.cs
│ ├── MathMultipleConverter.cs
│ ├── NotConverter.cs
│ ├── NotZeroConverter.cs
│ ├── NotZeroToVisibilityConverter.cs
│ ├── NullToVisibilityConverter.cs
│ ├── NullableDateTimeToCurrentDateConverter.cs
│ ├── PointValueConverter.cs
│ ├── RangeLengthConverter.cs
│ ├── RangePositionConverterConverter.cs
│ ├── ShadowConverter.cs
│ ├── ShadowEdgeConverter.cs
│ ├── ShadowInfo.cs
│ ├── SizeToRectConverter.cs
│ ├── SnackbarMessageTypeConverter.cs
│ ├── TextFieldHintVisibilityConverter.cs
│ └── TimeToVisibilityConverter.cs
│ ├── CustomPopupPlacementCallbackHelper.cs
│ ├── DataGridAssist.cs
│ ├── DateTimeEx.cs
│ ├── DialogClosingEventArgs.cs
│ ├── DialogClosingEventHandler.cs
│ ├── DialogHost.cs
│ ├── DialogHostEx.cs
│ ├── DialogOpenedEventArgs.cs
│ ├── DialogOpenedEventHandler.cs
│ ├── DialogSession.cs
│ ├── DpiHelper.cs
│ ├── DrawerHost.cs
│ ├── ExpanderAssist.cs
│ ├── Extensions.cs
│ ├── Flipper.cs
│ ├── HintAssist.cs
│ ├── HintProxyFabric.ComboBox.cs
│ ├── HintProxyFabric.PasswordBox.cs
│ ├── HintProxyFabric.TextBox.cs
│ ├── HintProxyFabric.cs
│ ├── IHintProxy.cs
│ ├── ISnackbarMessageQueue.cs
│ ├── Icon.cs
│ ├── IconType.cs
│ ├── ListBoxAssist.cs
│ ├── ListSortDirectionIndicator.cs
│ ├── ListViewAssist.cs
│ ├── MaterialDataGridComboBoxColumn.cs
│ ├── MaterialDataGridTextColumn.cs
│ ├── MaterialDateDisplay.cs
│ ├── MessageQueueExtension.cs
│ ├── PackIcon.cs
│ ├── PackIconDataFactory.cs
│ ├── PackIconExtension.cs
│ ├── PackIconKind.cs
│ ├── Palette.cs
│ ├── PaletteHelper.cs
│ ├── Plane3D.cs
│ ├── PopupBox.cs
│ ├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ └── Settings.Designer.cs
│ ├── RatingBar.cs
│ ├── RatingBarButton.cs
│ ├── Ripple.cs
│ ├── RippleAssist.cs
│ ├── ScaleHost.cs
│ ├── Screen.cs
│ ├── ScrollViewerAssist.cs
│ ├── ShadowAssist.cs
│ ├── SmartHint.cs
│ ├── Snackbar.cs
│ ├── SnackbarMessage.cs
│ ├── SnackbarMessageEventArgs.cs
│ ├── SnackbarMessageQueue.cs
│ ├── SnackbarMessageQueueItem.cs
│ ├── Spelling.cs
│ ├── StringExtensions.cs
│ ├── TextFieldAssist.cs
│ ├── ThemeAssist.cs
│ ├── TimePicker.cs
│ ├── ToggleButtonAssist.cs
│ ├── ToolTipAssist.cs
│ ├── Transitions
│ ├── CircleWipe.cs
│ ├── FadeWipe.cs
│ ├── ITransitionEffect.cs
│ ├── ITransitionEffectSubject.cs
│ ├── ITransitionWipe.cs
│ ├── IZIndexController.cs
│ ├── IndexedItemOffsetMultiplierExtension.cs
│ ├── SlideDirection.cs
│ ├── SlideOutWipe.cs
│ ├── SlideWipe.cs
│ ├── TransitionAssist.cs
│ ├── TransitionEffect.cs
│ ├── TransitionEffectBase.cs
│ ├── TransitionEffectExtension.cs
│ ├── TransitionEffectKind.cs
│ ├── TransitionEffectTypeConverter.cs
│ ├── Transitioner.cs
│ ├── TransitionerSlide.cs
│ ├── TransitionerSlideState.cs
│ ├── TransitioningContent.cs
│ └── TransitioningContentBase.cs
│ ├── TreeHelper.cs
│ ├── TreeViewAssist.cs
│ ├── Underline.cs
│ └── ValidationAssist.cs
├── Newtonsoft.Json.dll
└── log4net.dll
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/AnyExeToService.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28010.2050
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnyExeToService", "AnyExeToService\AnyExeToService.csproj", "{ADF4F772-01C7-4D07-A084-5F8069505323}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceApp", "ServiceApp\ServiceApp.csproj", "{5336321A-ED85-41BE-B03A-01EE82537455}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {ADF4F772-01C7-4D07-A084-5F8069505323}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {ADF4F772-01C7-4D07-A084-5F8069505323}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {ADF4F772-01C7-4D07-A084-5F8069505323}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {ADF4F772-01C7-4D07-A084-5F8069505323}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {5336321A-ED85-41BE-B03A-01EE82537455}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {5336321A-ED85-41BE-B03A-01EE82537455}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {5336321A-ED85-41BE-B03A-01EE82537455}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {5336321A-ED85-41BE-B03A-01EE82537455}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {C4B5404A-6CAA-4A6E-999B-B210967B4546}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/AnyExeToService/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/AnyExeToService/App.xaml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/AnyExeToService/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Reflection;
4 | using System.Windows;
5 |
6 | namespace AnyExeToService
7 | {
8 | ///
9 | /// App.xaml 的交互逻辑
10 | ///
11 | public partial class App : Application
12 | {
13 | static App()
14 | {
15 | File.WriteAllBytes("MaterialDesignThemes.Wpf.dll", AnyExeToService.Properties.Resources.MaterialDesignThemes_Wpf);
16 | File.WriteAllBytes("MaterialDesignColors.dll", AnyExeToService.Properties.Resources.MaterialDesignColors);
17 | AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
18 | }
19 |
20 | private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
21 | {
22 | return args.Name.Contains("Newtonsoft.Json") ? Assembly.Load(AnyExeToService.Properties.Resources.Newtonsoft_Json) : null;
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/AnyExeToService/ExeServiceConfig.cs:
--------------------------------------------------------------------------------
1 | namespace AnyExeToService
2 | {
3 | class ExeServiceConfig
4 | {
5 | public string Exe { get; set; }
6 | public string Argument { get; set; }
7 | public bool RedirectStandardInput { get; set; } = true;
8 | public bool RedirectStandardOutput { get; set; } = true;
9 | public bool UseShellExecute { get; set; } = false;
10 | public bool CreateNoWindow { get; set; } = true;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/AnyExeToService/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // 有关程序集的一般信息由以下
8 | // 控制。更改这些特性值可修改
9 | // 与程序集关联的信息。
10 | [assembly: AssemblyTitle("AnyExeToService")]
11 | [assembly: AssemblyDescription("将任意程序安装成Windows服务")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("HP Inc.")]
14 | [assembly: AssemblyProduct("AnyExeToService")]
15 | [assembly: AssemblyCopyright("Copyright © HP Inc. 2019")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | // 将 ComVisible 设置为 false 会使此程序集中的类型
20 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
21 | //请将此类型的 ComVisible 特性设置为 true。
22 | [assembly: ComVisible(false)]
23 |
24 | //若要开始生成可本地化的应用程序,请设置
25 | //.csproj 文件中的 CultureYouAreCodingWith
26 | //例如,如果您在源文件中使用的是美国英语,
27 | //使用的是美国英语,请将 设置为 en-US。 然后取消
28 | //对以下 NeutralResourceLanguage 特性的注释。 更新
29 | //以下行中的“en-US”以匹配项目文件中的 UICulture 设置。
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //主题特定资源词典所处位置
36 | //(未在页面中找到资源时使用,
37 | //或应用程序资源字典中找到时使用)
38 | ResourceDictionaryLocation.SourceAssembly //常规资源词典所处位置
39 | //(未在页面中找到资源时使用,
40 | //、应用程序或任何主题专用资源字典中找到时使用)
41 | )]
42 |
43 |
44 | // 程序集的版本信息由下列四个值组成:
45 | //
46 | // 主版本
47 | // 次版本
48 | // 生成号
49 | // 修订号
50 | //
51 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
52 | // 方法是按如下所示使用“*”: :
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.2.0")]
55 | [assembly: AssemblyFileVersion("1.0.2.0")]
56 |
--------------------------------------------------------------------------------
/AnyExeToService/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // 此代码由工具生成。
4 | // 运行时版本:4.0.30319.42000
5 | //
6 | // 对此文件的更改可能会导致不正确的行为,并且如果
7 | // 重新生成代码,这些更改将会丢失。
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace AnyExeToService.Properties {
12 |
13 |
14 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
15 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.6.0.0")]
16 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
17 |
18 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
19 |
20 | public static Settings Default {
21 | get {
22 | return defaultInstance;
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/AnyExeToService/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/AnyExeToService/Properties/app.manifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
47 |
54 |
55 |
69 |
--------------------------------------------------------------------------------
/AnyExeToService/Resources/MaterialDesignColors.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/AnyExeToService/Resources/MaterialDesignColors.dll
--------------------------------------------------------------------------------
/AnyExeToService/Resources/MaterialDesignThemes.Wpf.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/AnyExeToService/Resources/MaterialDesignThemes.Wpf.dll
--------------------------------------------------------------------------------
/AnyExeToService/Resources/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/AnyExeToService/Resources/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/AnyExeToService/Resources/ServiceApp.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/AnyExeToService/Resources/ServiceApp.exe
--------------------------------------------------------------------------------
/AnyExeToService/Resources/ServiceBridge.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/AnyExeToService/Resources/ServiceBridge.exe
--------------------------------------------------------------------------------
/AnyExeToService/Resources/install.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/AnyExeToService/Resources/install.exe
--------------------------------------------------------------------------------
/AnyExeToService/Resources/log4net.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/AnyExeToService/Resources/log4net.dll
--------------------------------------------------------------------------------
/AnyExeToService/main.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/AnyExeToService/main.ico
--------------------------------------------------------------------------------
/AnyExeToService/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/AnyExeToService/preferences_management_service_128px_1172109_easyicon.net.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/AnyExeToService/preferences_management_service_128px_1172109_easyicon.net.ico
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 cxwl3sxl
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AnyExeToService
将任意exe程序安装成windows服务
屏幕截图

[下载地址](https://github.com/cxwl3sxl/AnyExeToService/releases "下载地址")
# 原理
基于Instsrv.exe和srvany.exe实现
--------------------------------------------------------------------------------
/ServiceApp/ExeProcess.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.IO;
4 | using System.Threading;
5 | using log4net;
6 |
7 | namespace ServiceApp
8 | {
9 | class ExeProcess
10 | {
11 | private readonly ServiceConfig _config;
12 | private Process _mainProcess;
13 | private readonly ILog _log = LogManager.GetLogger(typeof(ExeProcess));
14 | private bool _stop = false;
15 | public ExeProcess(ServiceConfig config)
16 | {
17 | _config = config ?? throw new Exception("配置对象不能为空!");
18 | }
19 |
20 | public void Start()
21 | {
22 | StartProcess();
23 | ThreadPool.QueueUserWorkItem(ProcessEye);
24 | _log.Info("服务已经成功启动.");
25 | }
26 |
27 | private void _mainProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
28 | {
29 | _log.Debug(e.Data);
30 | }
31 |
32 | void StartProcess()
33 | {
34 | _mainProcess?.Kill();
35 | _mainProcess?.Dispose();
36 | _log.Debug($"程序路径:{_config.Exe}");
37 | _log.Debug($"启动参数:{_config.Argument}");
38 | _mainProcess = new Process
39 | {
40 | StartInfo = new ProcessStartInfo(_config.Exe)
41 | {
42 | WorkingDirectory = Path.GetDirectoryName(_config.Exe) ?? throw new InvalidOperationException(),
43 | Arguments = _config.Argument,
44 | RedirectStandardInput = _config.RedirectStandardInput,
45 | RedirectStandardOutput = _config.RedirectStandardOutput,
46 | UseShellExecute = _config.UseShellExecute,
47 | CreateNoWindow = _config.CreateNoWindow
48 | }
49 | };
50 | if (_config.RedirectStandardOutput)
51 | _mainProcess.OutputDataReceived += _mainProcess_OutputDataReceived;
52 | _mainProcess.Start();
53 | _log.Info($"服务进程{Path.GetFileName(_config.Exe)}已经启动,进程ID:{_mainProcess.Id}");
54 | }
55 |
56 | void ProcessEye(object notUsed)
57 | {
58 | while (!_stop)
59 | {
60 | try
61 | {
62 | if (_mainProcess == null)
63 | {
64 | Thread.Sleep(1000);
65 | continue;
66 | }
67 | if (_mainProcess.HasExited)
68 | {
69 | _mainProcess.Dispose();
70 | _mainProcess = null;
71 | _log.Warn($"检测到服务进程结束,正在重启...");
72 | StartProcess();
73 | }
74 | else
75 | {
76 | Thread.Sleep(500);
77 | }
78 | }
79 | catch (Exception ex)
80 | {
81 | _log.Warn($"监测出错", ex);
82 | }
83 | }
84 | }
85 |
86 | public void Stop()
87 | {
88 | _stop = true;
89 | _mainProcess?.Kill();
90 | _log.Info("服务已经成功停止.");
91 | }
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/ServiceApp/MainService.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace ServiceApp
2 | {
3 | partial class MainService
4 | {
5 | ///
6 | /// 必需的设计器变量。
7 | ///
8 | private System.ComponentModel.IContainer components = null;
9 |
10 | ///
11 | /// 清理所有正在使用的资源。
12 | ///
13 | /// 如果应释放托管资源,为 true;否则为 false。
14 | protected override void Dispose(bool disposing)
15 | {
16 | if (disposing && (components != null))
17 | {
18 | components.Dispose();
19 | }
20 | base.Dispose(disposing);
21 | }
22 |
23 | #region 组件设计器生成的代码
24 |
25 | ///
26 | /// 设计器支持所需的方法 - 不要修改
27 | /// 使用代码编辑器修改此方法的内容。
28 | ///
29 | private void InitializeComponent()
30 | {
31 | components = new System.ComponentModel.Container();
32 | this.ServiceName = "MainService";
33 | }
34 |
35 | #endregion
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/ServiceApp/MainService.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Reflection;
4 | using System.ServiceProcess;
5 | using log4net;
6 | using Newtonsoft.Json;
7 |
8 | namespace ServiceApp
9 | {
10 | partial class MainService : ServiceBase
11 | {
12 | private readonly ServiceConfig _config;
13 | private readonly ExeProcess _exeProcess;
14 | private readonly ILog _log = LogManager.GetLogger(typeof(MainService));
15 | public MainService()
16 | {
17 | InitializeComponent();
18 | var appName = Assembly.GetExecutingAssembly().Location;
19 | var configFile = $"{appName}.json";
20 | if (!File.Exists(configFile)) return;
21 | var configContent = File.ReadAllText(configFile);
22 | try
23 | {
24 | _config = JsonConvert.DeserializeObject(configContent);
25 | if (!File.Exists(_config.Exe)) throw new Exception($"指定的启动程序{_config.Exe}不存在");
26 | _exeProcess = new ExeProcess(_config);
27 | }
28 | catch (Exception ex)
29 | {
30 | _log.Warn("读取服务配置文件出错", ex);
31 | _log.Warn($"配置文件地址为:{configFile}");
32 | _log.Warn($"配置文件内容为:{Environment.NewLine}{configContent}");
33 | }
34 | }
35 |
36 | protected override void OnStart(string[] args)
37 | {
38 | if (_config == null) return;
39 | try
40 | {
41 | _exeProcess?.Start();
42 | }
43 | catch (Exception ex)
44 | {
45 | _log.Warn("启动服务出错", ex);
46 | throw;
47 | }
48 | }
49 |
50 | protected override void OnStop()
51 | {
52 | if (_config == null) return;
53 | try
54 | {
55 | _exeProcess?.Stop();
56 | }
57 | catch (Exception ex)
58 | {
59 | _log.Warn("停止服务出错", ex);
60 | }
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/ServiceApp/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.IO;
4 | using System.Reflection;
5 | using System.ServiceProcess;
6 | using System.Threading;
7 | using log4net;
8 |
9 | namespace ServiceApp
10 | {
11 | class Program
12 | {
13 | static void Main(string[] args)
14 | {
15 | var processName = Process.GetCurrentProcess().ProcessName;
16 | var dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), processName);
17 | if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
18 | Trace.WriteLine($"当前正在使用的日志路径为:{dir}");
19 | var logConfig = $"{dir}\\{processName}.log.config";
20 | if (!File.Exists(logConfig))
21 | {
22 | File.WriteAllText(logConfig, Properties.Resources.log4net1.Replace("$DIR$", dir).Replace("\\", "\\\\"));
23 | }
24 |
25 | AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
26 | log4net.Config.XmlConfigurator.Configure(new Uri(logConfig));
27 |
28 | var log = LogManager.GetLogger(typeof(Program));
29 | try
30 | {
31 | var servicesToRun = new ServiceBase[] { new MainService() };
32 | ServiceBase.Run(servicesToRun);
33 | }
34 | catch (Exception ex)
35 | {
36 | log.Fatal("启动服务出错", ex);
37 | }
38 | }
39 |
40 | private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
41 | {
42 | if (args.Name.Contains("Newtonsoft.Json"))
43 | return Assembly.Load(Properties.Resources.Newtonsoft_Json);
44 | if (args.Name.Contains("log4net"))
45 | return Assembly.Load(Properties.Resources.log4net_dll);
46 | return null;
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/ServiceApp/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // 有关程序集的一般信息由以下
6 | // 控制。更改这些特性值可修改
7 | // 与程序集关联的信息。
8 | [assembly: AssemblyTitle("ServiceApp")]
9 | [assembly: AssemblyDescription("将任意程序安装成Windows服务的辅助程序")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("HP Inc.")]
12 | [assembly: AssemblyProduct("ServiceApp")]
13 | [assembly: AssemblyCopyright("Copyright © HP Inc. 2019")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // 将 ComVisible 设置为 false 会使此程序集中的类型
18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
19 | //请将此类型的 ComVisible 特性设置为 true。
20 | [assembly: ComVisible(false)]
21 |
22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23 | [assembly: Guid("5336321a-ed85-41be-b03a-01ee82537455")]
24 |
25 | // 程序集的版本信息由下列四个值组成:
26 | //
27 | // 主版本
28 | // 次版本
29 | // 生成号
30 | // 修订号
31 | //
32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
33 | // 方法是按如下所示使用“*”: :
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.2.0")]
36 | [assembly: AssemblyFileVersion("1.0.2.0")]
37 |
--------------------------------------------------------------------------------
/ServiceApp/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // 此代码由工具生成。
4 | // 运行时版本:4.0.30319.42000
5 | //
6 | // 对此文件的更改可能会导致不正确的行为,并且如果
7 | // 重新生成代码,这些更改将会丢失。
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace ServiceApp.Properties {
12 | using System;
13 |
14 |
15 | ///
16 | /// 一个强类型的资源类,用于查找本地化的字符串等。
17 | ///
18 | // 此类是由 StronglyTypedResourceBuilder
19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
20 | // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
21 | // (以 /str 作为命令选项),或重新生成 VS 项目。
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources {
26 |
27 | private static global::System.Resources.ResourceManager resourceMan;
28 |
29 | private static global::System.Globalization.CultureInfo resourceCulture;
30 |
31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
32 | internal Resources() {
33 | }
34 |
35 | ///
36 | /// 返回此类使用的缓存的 ResourceManager 实例。
37 | ///
38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
39 | internal static global::System.Resources.ResourceManager ResourceManager {
40 | get {
41 | if (object.ReferenceEquals(resourceMan, null)) {
42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ServiceApp.Properties.Resources", typeof(Resources).Assembly);
43 | resourceMan = temp;
44 | }
45 | return resourceMan;
46 | }
47 | }
48 |
49 | ///
50 | /// 重写当前线程的 CurrentUICulture 属性
51 | /// 重写当前线程的 CurrentUICulture 属性。
52 | ///
53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
54 | internal static global::System.Globalization.CultureInfo Culture {
55 | get {
56 | return resourceCulture;
57 | }
58 | set {
59 | resourceCulture = value;
60 | }
61 | }
62 |
63 | ///
64 | /// 查找 System.Byte[] 类型的本地化资源。
65 | ///
66 | internal static byte[] log4net_dll {
67 | get {
68 | object obj = ResourceManager.GetObject("log4net_dll", resourceCulture);
69 | return ((byte[])(obj));
70 | }
71 | }
72 |
73 | ///
74 | /// 查找类似 <?xml version="1.0" encoding="utf-8" ?>
75 | ///
76 | ///<configuration>
77 | /// <configSections>
78 | /// <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
79 | /// </configSections>
80 | /// <log4net>
81 | /// <appender name="RootAppender" type="log4net.Appender.RollingFileAppender">
82 | /// <param name="File" value="#ROOT#\\" />
83 | /// <param name="AppendToFile" value="true" />
84 | /// <param name="MaxFileSize" value="10240" />
85 | /// <param name="MaxSizeRollBackups" value="100" />
86 | /// <param name="S [字符串的其余部分被截断]"; 的本地化字符串。
87 | ///
88 | internal static string log4net1 {
89 | get {
90 | return ResourceManager.GetString("log4net1", resourceCulture);
91 | }
92 | }
93 |
94 | ///
95 | /// 查找 System.Byte[] 类型的本地化资源。
96 | ///
97 | internal static byte[] Newtonsoft_Json {
98 | get {
99 | object obj = ResourceManager.GetObject("Newtonsoft_Json", resourceCulture);
100 | return ((byte[])(obj));
101 | }
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/ServiceApp/Resources/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/ServiceApp/Resources/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/ServiceApp/Resources/log4net.dll.dat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/ServiceApp/Resources/log4net.dll.dat
--------------------------------------------------------------------------------
/ServiceApp/ServiceApp.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {5336321A-ED85-41BE-B03A-01EE82537455}
8 | Exe
9 | ServiceApp
10 | ServiceApp
11 | v4.0
12 | 512
13 | true
14 |
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | ..\AnyExeToService\Resources\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 | False
38 | ..\packages\log4net.dll
39 |
40 |
41 | False
42 | ..\packages\Newtonsoft.Json.dll
43 | False
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 | Component
60 |
61 |
62 | MainService.cs
63 |
64 |
65 |
66 |
67 | True
68 | True
69 | Resources.resx
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | ResXFileCodeGenerator
79 | Resources.Designer.cs
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
--------------------------------------------------------------------------------
/ServiceApp/ServiceConfig.cs:
--------------------------------------------------------------------------------
1 | namespace ServiceApp
2 | {
3 | class ServiceConfig
4 | {
5 | public string Exe { get; set; }
6 | public string Argument { get; set; }
7 | public bool RedirectStandardInput { get; set; } = true;
8 | public bool RedirectStandardOutput { get; set; } = true;
9 | public bool UseShellExecute { get; set; } = false;
10 | public bool CreateNoWindow { get; set; } = true;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/ServiceApp/log4net.config:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/images/index.PNG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/images/index.PNG
--------------------------------------------------------------------------------
/packages/MaterialDesignColors.1.1.2/.signature.p7s:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/packages/MaterialDesignColors.1.1.2/.signature.p7s
--------------------------------------------------------------------------------
/packages/MaterialDesignColors.1.1.2/MaterialDesignColors.1.1.2.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/packages/MaterialDesignColors.1.1.2/MaterialDesignColors.1.1.2.nupkg
--------------------------------------------------------------------------------
/packages/MaterialDesignColors.1.1.2/lib/net45/MaterialDesignColors.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/packages/MaterialDesignColors.1.1.2/lib/net45/MaterialDesignColors.dll
--------------------------------------------------------------------------------
/packages/MaterialDesignThemes.2.5.1/.signature.p7s:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/packages/MaterialDesignThemes.2.5.1/.signature.p7s
--------------------------------------------------------------------------------
/packages/MaterialDesignThemes.2.5.1/MaterialDesignThemes.2.5.1.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/packages/MaterialDesignThemes.2.5.1/MaterialDesignThemes.2.5.1.nupkg
--------------------------------------------------------------------------------
/packages/MaterialDesignThemes.2.5.1/lib/net45/MaterialDesignThemes.Wpf.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cxwl3sxl/AnyExeToService/3e0617e7affdfbb1108acb9fab2bf8b59f1e2bbd/packages/MaterialDesignThemes.2.5.1/lib/net45/MaterialDesignThemes.Wpf.dll
--------------------------------------------------------------------------------
/packages/MaterialDesignThemes.2.5.1/src/net45/Badged.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using System.Windows;
4 | using System.Windows.Media.Animation;
5 | using ControlzEx;
6 |
7 | namespace MaterialDesignThemes.Wpf
8 | {
9 | [TemplatePart(Name = BadgeContainerPartName, Type = typeof(UIElement))]
10 | public class Badged : BadgedEx
11 | {
12 | public static readonly DependencyProperty BadgeChangedStoryboardProperty = DependencyProperty.Register(
13 | "BadgeChangedStoryboard", typeof(Storyboard), typeof(Badged), new PropertyMetadata(default(Storyboard)));
14 |
15 | public Storyboard BadgeChangedStoryboard
16 | {
17 | get { return (Storyboard)this.GetValue(BadgeChangedStoryboardProperty); }
18 | set { this.SetValue(BadgeChangedStoryboardProperty, value); }
19 | }
20 |
21 | static Badged()
22 | {
23 | DefaultStyleKeyProperty.OverrideMetadata(typeof(Badged), new FrameworkPropertyMetadata(typeof(Badged)));
24 | }
25 |
26 | public static readonly DependencyProperty BadgeColorZoneModeProperty = DependencyProperty.Register(
27 | "BadgeColorZoneMode", typeof(ColorZoneMode), typeof(Badged), new PropertyMetadata(default(ColorZoneMode)));
28 |
29 | public ColorZoneMode BadgeColorZoneMode
30 | {
31 | get { return (ColorZoneMode) GetValue(BadgeColorZoneModeProperty); }
32 | set { SetValue(BadgeColorZoneModeProperty, value); }
33 | }
34 |
35 | public override void OnApplyTemplate()
36 | {
37 | this.BadgeChanged -= this.OnBadgeChanged;
38 |
39 | base.OnApplyTemplate();
40 |
41 | this.BadgeChanged += this.OnBadgeChanged;
42 | }
43 |
44 | private void OnBadgeChanged(object sender, RoutedPropertyChangedEventArgs