2

您好,我想在 C Sharp 中使用 FileSystemWatcher 来监视文件夹中的文本文件 Read There Text 并将它们的文本上传到带有 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();
    }
}
}
4

1 回答 1

0

要处理多个更改通知,请转到此处并查找 @BaBu 的答案。

另外,
如果您只需要从文件中读取,
您是否尝试以共享模式打开它?

于 2013-05-26T13:53:58.743 回答