版本兼容
<startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0"/><supportedRuntime version="v2.0.50727"/></startup>
查询目前系统所用的.net framework版本
reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\full" /v version
.NET Framework 4.x 程序到底运行在哪个 CLR 版本之上
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.IO;using System.Linq;using System.ServiceProcess;using System.Text;using System.Threading.Tasks;namespace TestWindowsService{public partial class Service1 : ServiceBase{public Service1(){InitializeComponent();}protected override void OnStart(string[] args){try{this.WriteLog("Start Service!");this.ToDoSomeThings();}catch(Exception ex){}}protected override void OnStop(){}private void ToDoSomeThings(){DateTime dateTime = DateTime.Now;if(dateTime.Hour == 14 && dateTime.Minute == 30){int a = 0;for(int i=0;i<1000; i++){a += i;}this.WriteLog(a.ToString());}}private void WriteLog(string LogContent){string FilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\Log.txt";FileStream fileStream = null;if(File.Exists(FilePath)){fileStream = new FileStream(FilePath, FileMode.Append);}else{fileStream = new FileStream(FilePath, FileMode.OpenOrCreate);}StreamWriter streamWriter = new StreamWriter(fileStream);streamWriter.WriteLine(DateTime.Now + System.Environment.NewLine + LogContent);streamWriter.Close();fileStream.Close();GC.Collect();}}}
要以管理员的身份启动powershell或者cmd
安装服务
C:\windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe D:\workspace\workspace_net\TestWindowsService\TestWindowsService\bin\Debug\TestWindowsService.exe
卸载服务
C:\windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /u D:\workspace\workspace_net\TestWindowsService\TestWindowsService\bin\Debug\TestWindowsService.exe
启动服务
net start service_name
停止服务
net stop service_name

