2010-05-21 74 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來接收和顯示跟蹤消息。