2013-05-26 32 views
2

你好,我想使用FileSystemWatcher的用C夏普觀看文本文件來文件夾中的閱讀有文本,並將其上傳文本到Web服務器獲取已在使用的文件的錯誤與C夏普讀取文本文件中FileSystemWatcher的在C#中的另一個資源

但問題是,當我嘗試它和第一次打開一些文件時,它工作正常,但第二次當文件來到目錄時,它會告訴我該文件已經由另一個應用程序使用,或者資源不是已分配的空閒資源。

這裏是小的代碼爲它

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
using System.Net; 

namespace FileChangeNotifier 
{ 
public partial class frmNotifier : Form 
{ 
    private StringBuilder m_Sb; 
    private bool m_bDirty; 
    private System.IO.FileSystemWatcher m_Watcher; 
    private bool m_bIsWatching; 

    public frmNotifier() 
    { 
     InitializeComponent(); 
     m_Sb = new StringBuilder(); 
     m_bDirty = false; 
     m_bIsWatching = false; 
    } 

    private void btnWatchFile_Click(object sender, EventArgs e) 
    { 
     if (m_bIsWatching) 
     { 
      m_bIsWatching = false; 
      m_Watcher.EnableRaisingEvents = false; 
      m_Watcher.Dispose(); 
      btnWatchFile.BackColor = Color.LightSkyBlue; 
      btnWatchFile.Text = "Start Watching"; 

     } 
     else 
     { 
      m_bIsWatching = true; 
      btnWatchFile.BackColor = Color.Red; 
      btnWatchFile.Text = "Stop Watching"; 

      m_Watcher = new System.IO.FileSystemWatcher(); 
      if (rdbDir.Checked) 
      { 
       m_Watcher.Filter = "*.*"; 
       m_Watcher.Path = txtFile.Text + "\\"; 
      } 
      else 
      { 
       m_Watcher.Filter = txtFile.Text.Substring(txtFile.Text.LastIndexOf('\\') + 1); 
       m_Watcher.Path = txtFile.Text.Substring(0, txtFile.Text.Length - m_Watcher.Filter.Length); 
      } 

      if (chkSubFolder.Checked) 
      { 
       m_Watcher.IncludeSubdirectories = true; 
      } 

      m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; 
      m_Watcher.Created += new FileSystemEventHandler(OnChanged); 
      m_Watcher.EnableRaisingEvents = true; 
     } 
    } 

    private void OnChanged(object sender, FileSystemEventArgs e) 
    { 
     if (!m_bDirty) 
     { 
      readFile(e.FullPath); 
      m_Sb.Remove(0, m_Sb.Length); 
      m_Sb.Append(e.FullPath); 
      m_Sb.Append(" "); 
      m_Sb.Append(e.ChangeType.ToString()); 
      m_Sb.Append(" "); 
      m_Sb.Append(DateTime.Now.ToString()); 
      m_bDirty = true; 
     } 
    } 


    private void readFile(String filename) { 
     String line = ""; 
     if (File.Exists(filename)) 
     { 
      try{ 
       StreamReader sr = new StreamReader(filename); 

       //code for multiline reading but i need only one line so i am going to change he code 
       /* while ((line = sr.ReadLine()) != null) 
       { 
        MessageBox.Show(line); 
       } 
       */ 
       line = sr.ReadLine(); 
       MessageBox.Show(line); 
       uploadDataToServer(line); 
       sr.Close(); 
      } catch(IOException e){ 
       MessageBox.Show(e.Message); 

      } 

     } 

    } 

    private void uploadDataToServer(String data) { 
     String url = "http://209.90.88.135/~lilprogr/?data="+data; 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

     Stream resStream = response.GetResponseStream(); 
    } 
} 
} 
+0

可能會有一些文件處理程序處於保持。請檢查一下。 – Saravanan

+0

如何檢查? –

+0

只是嘗試使用下面的代碼在一個私人的方法,如果返回false,你可以使用文件。 '嘗試 { 流= file.Open(FileMode.Open,FileAccess.ReadWrite,FileShare.None); } 趕上(IOException異常) { //該文件是不可用的,因爲它是: //被寫入 //或通過其它工藝被處理或線程等。 返回真; } return false;' – Saravanan

回答

0

要處理多個更改通知,去here並查找@巴布的答案。

此外,
如果你需要的是從文件中讀取,
您是否嘗試過在Shared Mode打開它?

相關問題