如何解决文件仅在使用 nuget 包时被另一个进程使用
今天出现了一个奇怪的问题。我正在设置一个 FileSystemWatcher。一旦文件夹发生写入更改(文件夹中只有 1 个文件),我就会打开文件并读取 xml 条目。我将读取的代码打包到一个单独的 nuget 中,因为我在多个项目中使用它。为了触发problem,我在记事本中打开xml 文件,对其进行编辑并保存。这是负责读取xml的代码:
public bool GetBoolean(string value)
{
using (FileStream fileStream = File.Open(this.confFile,FileMode.Open,FileAccess.Read))
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load((Stream) fileStream);
XmlNode xmlNode = xmlDocument.DocumentElement.SelectSingleNode("/path/" + value);
return Convert.ToBoolean(xmlNode.InnerText);
}
}
这是更改监听代码:
void StartListeningForChanges(){
void OnChanged(object source,FileSystemEventArgs e){
Console.WriteLine("{0},with path {1} has been {2}",e.Name,e.FullPath,e.ChangeType);
bool whatisit= Someclass.GetBoolean("someentry");
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.IncludeSubdirectories = false;
watcher.Path = confFolder;
watcher.Filter = "*.*";
watcher.Changed += OnChanged;
watcher.EnableRaisingEvents = true;
}
如果 OnChanged 使用来自 nuget 包的代码,我会收到错误消息 "The process cannot access the file because it is being used by another process"
。如果我使用相同的代码,但直接将其粘贴到项目中:
void StartListeningForChanges(){
void OnChanged(object source,e.ChangeType);
using (FileStream fileStream = File.Open(this.confFile,FileAccess.Read))
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load((Stream) fileStream);
XmlNode xmlNode = xmlDocument.DocumentElement.SelectSingleNode("/fullpath");
// do something with xmlNode.InnerText
}
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.IncludeSubdirectories = false;
watcher.Path = confFolder;
watcher.Filter = "*.*";
watcher.Changed += OnChanged;
watcher.EnableRaisingEvents = true;
}
不会抛出任何错误,就好像该项目使用的是未使用只读流的先前版本的 nuget 包。不过 IDE 确实显示了正确的版本。
可能是什么问题?由于未来的更新,我想继续使用 nuget 包。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。