gpt4 book ai didi

c# - 在 C# 中的 FileSystemWatcher 中读取文本文件时出现文件已被另一个资源使用的错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:13:12 25 4
gpt4 key购买 nike

你好,我想在 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();
}
}
}

最佳答案

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

此外,
如果你只需要从文件中读取,
你试过在Shared Mode打开吗?

关于c# - 在 C# 中的 FileSystemWatcher 中读取文本文件时出现文件已被另一个资源使用的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16758048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com