日期:2014-05-17  浏览次数:20457 次

windows service

在调试的时候,c:\ComputerInfo.txt 可以生成,为什么启动服务后,不行了




        private void timer1_Tick(object sender, EventArgs e)
        {
            //本机电脑硬件配置文件 
            string strFileName = @"c:\ComputerInfo.txt";
            string strTest = "硬盘信息+显卡信息+主板信息+声卡信息+网卡信息+打印机信息";
            //判断传送文件是否存在.如果不存在则写入一行测试信息 
            if (!File.Exists(strFileName))
            {
                StreamWriter sw = File.CreateText(strFileName);
                sw.Close();
            }
            byte[] content = Encoding.UTF8.GetBytes(strTest);
            //创建文本文件对象 
            FileStream fs = new FileStream(strFileName, FileMode.Append);
            //文件中写入测试数据 
            fs.Write(content, 0, content.Length);
            //关闭文件对象 
            fs.Close();

        }

------解决方案--------------------
改用System.Timers.Timer及Elapsed事件
------解决方案--------------------
MSDN开头介绍就说了
System.Windows.Forms.Timer 类实现按用户定义的时间间隔引发事件的计时器。此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用。