我寫(xiě)了個(gè)windos往文件夾里寫(xiě)東西,但是不行啊,代碼如下:
Installer1.Designer.cs中的
??????? private System.ComponentModel.IContainer components = null;??????? private System.ServiceProcess.ServiceProcessInstaller spInstaller;??????? private System.ServiceProcess.ServiceInstaller sInstaller;??????? /// <summary> ??????? /// /// 清理所有正在使用的資源。 ??????? /// /// </summary>???? ??????? /// /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>? ??????? protected override void Dispose(bool disposing)??????? {??????????? if (disposing && (components != null))??????????? {??????????????? components.Dispose();??????????? }??????????? base.Dispose(disposing);??????? }
??????? /// <summary>???????? #region 組件設(shè)計(jì)器生成的代碼??????? /// /// 設(shè)計(jì)器支持所需的方法 - 不要?? ??????? /// /// 使用代碼編輯器修改此方法的內(nèi)容。? ??????? /// /// </summary>????? ??????? private void InitializeComponent()??????? {??????????? components = new System.ComponentModel.Container();??????????? // 創(chuàng)建ServiceProcessInstaller對(duì)象和ServiceInstaller對(duì)象??????? ??????????? this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();??????????? this.sInstaller = new System.ServiceProcess.ServiceInstaller();??????????? // 設(shè)定ServiceProcessInstaller對(duì)象的帳號(hào)、用戶(hù)名和密碼等信息?? ??????????? this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;??????????? this.spInstaller.Password = null;??????????? this.spInstaller.Username = null;??????????? // 設(shè)定服務(wù)的名稱(chēng)?????????? ??????????? this.sInstaller.ServiceName = "WindowsService1";??????????? //設(shè)定服務(wù)啟動(dòng)的方式???????? ??????????? this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;??????????? this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });??????? }在創(chuàng)建的安裝程序類(lèi)中是
??????? Timer time;??????? public Service1()??????? {??????????? InitializeComponent();??????? }
??????? protected override void OnStart(string[] args)??????? {??????????? time = new Timer(1000);
???? time.Start();
??????????? time.Elapsed += new ElapsedEventHandler(time_Elapsed);
??????? }
??????? void time_Elapsed(object sender, ElapsedEventArgs e)??????? {??????????? string filePath = AppDomain.CurrentDomain.BaseDirectory + "test.txt"; ??????????? StreamWriter sw = null;??????????? if (!File.Exists(filePath)) { ??????????????? sw = File.CreateText(filePath); ??????????? } else {??????????????? sw = File.AppendText(filePath);??????????? } ??????????? sw.Write("訪問(wèn)時(shí)間:" + DateTime.Now.ToString() + Environment.NewLine); sw.Close();??????? }
??????? protected override void OnStop()??????? {??????????? time.Stop(); ??????????? time.Dispose();??????? }但是我吧這個(gè)服務(wù)添加到window服務(wù)中,服務(wù)也啟動(dòng)了,但是文件沒(méi)有生產(chǎn)。怎么回事啊。
windos服務(wù)實(shí)現(xiàn)定時(shí)寫(xiě)文件
鴻蒙傳說(shuō)
2018-12-06 23:52:33