C# writing windows Service to monitor computer shutdown

When writing a file, the C drive requires administrator permissions to modify the permissions of the folder where the txt file is located


namespace WindowsService1
{
    
    
	public partial class Service1 : ServiceBase
	{
    
    
		public Service1()
		{
    
    
			InitializeComponent();
		}
		protected void OnShutdown()///关机事件
		{
    
    
			string path=@"C:\shutdownlog.txt";
			string msg=System.Environment.UserDomainName+"于"+DateTime.Now.ToString("mm月DD日hh时mm分ss秒")+"执行关机任务";
			System.IO.File.AppendAllText(path,msg);
			base.OnShutdown();
		}
}

Guess you like

Origin blog.csdn.net/qq_39072819/article/details/109672243