在c#的FileSystemWatcher中读取文本文件,得到文件已被其他资源使用的错误

本文关键字:文件 其他 错误 资源 FileSystemWatcher 读取 取文本 | 更新日期: 2023-09-27 18:15:17

你好,我想使用C Sharp中的FileSystemWatcher来监视文件夹中的文本文件阅读那里的文本并将其文本上传到带有C Sharp GET请求的Web服务器

,但问题是,当我尝试它和第一次当一些文件打开它工作正常,但在第二次当一个文件来到目录,它会告诉我,该文件已经被另一个应用程序使用或资源不是自由的,它已经分配。

这里是它的小代码

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();
    }
}
}

在c#的FileSystemWatcher中读取文本文件,得到文件已被其他资源使用的错误

要处理多个更改通知,请到这里寻找@BaBu的答案。

,
如果只需要从文件中读取,则
你试过以共享模式打开它吗?