如何在窗口服务中创建文本文件

时间:2010-05-21 07:18:20

标签: c#

我有一个XML文件

<config>
<ServiceName>autorunquery</ServiceName>
<DBConnection>
    <server>servername</server>
    <user>xyz</user>
    <password>klM#2bs</password>
<initialcatelog>TEST</initialcatelog>

</DBConnection>
<Log>
    <logfilename>d:\testlogfile.txt</logfilename>
</Log>
<Frequency>
    <value>10</value>
    <unit>minute</unit>
</Frequency>
<CheckQuery>select * from credit_debit1 where station='Corporate'</CheckQuery>
<Queries total="3">
    <Query id="1">Update credit_debit1 set station='xxx' where id=2</Query>
<Query id="2">Update credit_debit1 set station='xxx' where id=4</Query>
<Query id="3">Update credit_debit1 set station='xxx' where id=9</Query>

</Queries>
</config>




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Xml;

namespace Service1
{
    public partial class Service1 : ServiceBase
    {
        XmlTextReader reader = null;
        string path = null;
        FileStream fs = null;
        StreamWriter sw = null;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timer1.Enabled = true;
            timer1.Interval = 10000;
            timer1.Start();
            logfile("start service");


        }

        protected override void OnStop()
        {
            timer1.Enabled = false;
            timer1.Stop();
            logfile("stop service");

        }
        private void logfile(string content)
        {

            try
            {
                reader = new XmlTextReader("queryconfig.xml");//xml file name which is in current directory
                if (reader.ReadToFollowing("logfilename"))
                {
                    path = reader.ReadElementContentAsString();
                }
                fs = new FileStream(path, FileMode.Append, FileAccess.Write);
                sw = new StreamWriter(fs);
                sw.Write(content);
                sw.WriteLine(DateTime.Now.ToString());

            }
            catch (Exception ex)
            {
                sw.Write(ex.ToString());
                throw;
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (sw != null)
                    sw.Close();
                if (fs != null)
                    fs.Close();
            }
        }

    }
}

我的问题是没有创建文件。

3 个答案:

答案 0 :(得分:0)

我认为这可能是因为你使用的是System.Windows.Forms.Timer。它不适用于Windows服务。 将您的计时器组件更改为System.Timers.Timer。此类适用于Windows服务。

答案 1 :(得分:0)

我认为服务标识无权写入HD。 检查系统事件日志中的异常。

答案 2 :(得分:0)

如果您的文件未创建,您很可能会收到一个异常,该异常将出现在事件日志中,并且您的服务将终止,因为您不处理任何异常。

我的猜测是文件已创建但不在您期望的位置。要检查是使用硬编码的绝对路径还是使用Process Explorer查找服务的工作文件夹。

调试此类方案的一种简单方法是使用System.Diagnostics.Trace.WriteLine()发出调试信息,然后从Sysinternals启动Debug View以接收和显示跟踪消息。