我有一个应用程序,我正在寻找一个文本文件,如果对文件进行任何更改,我使用OnChangedeventhandler来处理事件.我正在使用NotifyFilters.LastWriteTime但事件仍被解雇两次.这是代码.
public void Initialize()
{
FileSystemWatcher _fileWatcher = new FileSystemWatcher();
_fileWatcher.Path = "C:\\Folder";
_fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
_fileWatcher.Filter = "Version.txt";
_fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
_fileWatcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
.......
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下OnChanged,当我更改文本文件version.txt并保存时,会调用两次.
基类实例化MEF目录和容器,并将它们保存为类变量,以便稍后进行参照访问
public class FieldProcessor
{
private CompositionContainer _container;
private DirectoryCatalog dirCatalog;
public FieldProcessor()
{
var catalog = new AggregateCatalog();
//Adds all the parts found in the same assembly as the TestPlugin class
catalog.Catalogs.Add(new AssemblyCatalog(typeof(TestPlugin).Assembly));
dirCatalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory + "Plugin\\");
catalog.Catalogs.Add(dirCatalog);
//Create the CompositionContainer with the parts in the catalog
_container = new CompositionContainer(catalog);
}
public void refreshCatalog()
{
dirCatalog.Refresh();
}
} ...
Run Code Online (Sandbox Code Playgroud)
这是我试图覆盖的插件.我的更新测试是,返回的stings输出到文本框,我更改了插件返回的字符串,重建并将其复制到插件文件夹中.但它不会更新正在运行的应用程序,直到我关闭并重新启动应用程序.
[Export(typeof(IPlugin))]
[ExportMetadata("PluginName", "TestPlugin2")]
public class TestPlugin2 : IPlugin
{
public IEnumerable<IField> …Run Code Online (Sandbox Code Playgroud) 我正在寻找在.NET中完成热交换的良好实现.我需要的是:
我一直在研究MEF及其目录加载机制,但它似乎非常不可靠.也许有人在那里有另一种实现方式?