【发布时间】:2019-01-31 18:43:57
【问题描述】:
我正在使用FileSystemWatcher 来检测文件。一旦它检测到文件,我想读取内容并将其存储在一个字符串中。但是当试图阅读内容时,我得到的进程已经在使用中。
这是我的代码:
var watcher = new FileSystemWatcher(b);
watcher.EnableRaisingEvents = true;
watcher.Path = Path.GetDirectoryName(b + "CMDExec.txt");
watcher.Filter = Path.GetFileName(b + "CMDExec.txt");
watcher.Created += Watcher_Created;
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
string b = Path.GetTempPath();
string text = System.IO.File.ReadAllText(b + "CMDExec.txt");
}
【问题讨论】:
-
史蒂夫的回答很中肯,所以我不会提供额外的详细说明。但是,我认为将您指向最容易解决您的问题的思考过程很有用:See this Q&A 是一个很好的起点。
标签: c# filesystemwatcher system.io.file