首页 新闻 赞助 找找看

windos服务实现定时写文件

0
[已解决问题] 解决于 2012-03-17 17:52

我写了个windos往文件夹里写东西,但是不行啊,代码如下:

Installer1.Designer.cs中的

        private System.ComponentModel.IContainer components = null;
        private System.ServiceProcess.ServiceProcessInstaller spInstaller;
        private System.ServiceProcess.ServiceInstaller sInstaller;
        /// <summary>
        /// /// 清理所有正在使用的资源。
        /// /// </summary>    
        /// /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        /// <summary>         #region 组件设计器生成的代码
        /// /// 设计器支持所需的方法 - 不要  
        /// /// 使用代码编辑器修改此方法的内容。 
        /// /// </summary>     
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            // 创建ServiceProcessInstaller对象和ServiceInstaller对象       
            this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            this.sInstaller = new System.ServiceProcess.ServiceInstaller();
            // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息  
            this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
            this.spInstaller.Password = null;
            this.spInstaller.Username = null;
            // 设定服务的名称          
            this.sInstaller.ServiceName = "WindowsService1";
            //设定服务启动的方式        
            this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
            this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.spInstaller, this.sInstaller });
        }
在创建的安装程序类中是

        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("访问时间:" + DateTime.Now.ToString() + Environment.NewLine); sw.Close();
        }

        protected override void OnStop()
        {
            time.Stop();
            time.Dispose();
        }
但是我吧这个服务添加到window服务中,服务也启动了,但是文件没有生产。怎么回事啊。

rains的主页 rains | 小虾三级 | 园豆:860
提问于:2011-01-13 12:08
< >
分享
最佳答案
0
邀月 | 高人七级 |园豆:25475 | 2011-01-13 13:10
你这里面的attach to process 那个图片中的工具是那个啊,
rains | 园豆:860 (小虾三级) | 2011-01-13 14:35
@水淼:附加到进程
邀月 | 园豆:25475 (高人七级) | 2011-01-13 18:09
其他回答(1)
0

你的timer 没有打开定时执行

 timer1.Start();

邢少 | 园豆:10926 (专家六级) | 2011-01-13 14:58
我加上了,但是不行还是,对吗
支持(0) 反对(0) rains | 园豆:860 (小虾三级) | 2011-01-13 16:10
@水淼:你的timer是那个timer? .net中有3个timer组件。〔详细使googel一下〕 System.Windows.Forms.Timer System.Threading.Timer System.Timers.Timer 在服务中你应该用System.Timers.Timer 。
支持(0) 反对(0) 邢少 | 园豆:10926 (专家六级) | 2011-01-14 08:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册