2010-05-21 80 views
0

创建文本文件,我有一个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(); 
      } 
     } 

    } 
} 

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

+1

不回答你的问题,但只是一些一般性的建议。除非确定创建了'sw',否则不应该在异常处理程序中调用'sw.Write',但通常在日志记录失败时尝试记录错误是个不错的主意:)。如果你只在logging方法中使用'reader'和'sw',你可以将它们变成局部变量而不是成员变量。如果你这样做,你可以使用'using'而不是finally语句。 – 2010-05-21 07:40:18

回答

0

我认为这很可能是因为你正在使用System.Windows.Forms.Timer。它的设计不适用于Windows服务。 将您的计时器组件更改为System.Timers.Timer。该课程适用于Windows服务。

+0

啊是的,Forms.Timer需要消息循环,无窗口的Windows服务缺乏。 – 2010-05-21 07:29:01

0

我想服务标识无权写入HD。 检查系统事件日志是否有例外情况。

0

如果你的文件没有创建,你很可能会得到一个异常,它将出现在事件日志中,并且你的服务将会终止,因为你没有处理任何异常。

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

一个简单的调试场景的技术是使用System.Diagnostics.Trace.WriteLine()来输出调试信息,然后从Sysinternals启动Debug View来接收和显示跟踪消息。