当用户修改MySolution.main.config(我的app.config)时,我想重新阅读它。感谢FileSystemWatcher。我构建了一个名为FileWatcher的包装器。
用这段代码捕获OnChanged事件
var map = new ExeConfigurationFileMap { ExeConfigFilename = _appConfigFilePath };
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
引发此异常
系统。配置ConfigurationErrorsException HResult=0x80131902消息=加载配置文件时出错:进程无法访问该文件'C:\Source\Solutions\MySolution_1.2.3\MySolution\bin\Debug\MySolution.main.config'因为它正被另一个进程使用。(C:\Source\Solutions\MySolution_1.2.3\MySolution\bin\Debug\MySolution.main.config)来源=系统。配置ConfigurationManager StackTrace:位于系统配置ConfigurationSchemaErrors。ThrowIfErrors(布尔值ignoreLocal)//src/libraries/System。配置ConfigurationManager/src/System/Configuration/ConfigurationsSchemaErrors.cs:line71 at系统配置BaseConfigurationRecord。ThrowIfParseErrors(配置架构错误schemaErrors)//src/libraries/System。配置ConfigurationManager/src/System/Configuration/BaseConfigurationRecord.cs:line3634在System。配置配置ctor(字符串locationSubPath,类型typeConfigHost,对象[]hostInitConfigurationParams)//src/libraries/System。配置ConfigurationManager/src/System/Configuration/Configuration.cs:line75 at系统配置ClientConfigurationHost。OpenExeConfiguration(配置文件映射fileMap,布尔isMachine,ConfigurationUserLevel userLevel,字符串exePath)//src/libraries/System。配置ConfigurationManager/src/System/Configuration/ClientConfigurationHost.cs:line485 at系统配置ConfigurationManager。OpenExeConfigurationImpl(配置文件映射fileMap,布尔isMachine,ConfigurationUserLevel userLevel,字符串exePath,布尔预加载)//src/libraries/System。配置ConfigurationManager/src/System/Configuration/Configuration Manager.cs:line214系统配置ConfigurationManager。OpenMappedExeConfiguration(ExeConfigurationFileMapfileMap,ConfigurationUserLevel userLevel)//src/libraries/System。配置ConfigurationManager/src/System/Configuration/Configuration Manager.cs:line192 atSiav。MySolution。日志配置Util。FileWatcher。OnChanged(对象发件人,中的FileSystemEventArgs e)C: \Source\Solutions\MySolution_1.2.3\Siav.MySolution.Log\ConfigurationUtil\FileWatcher.cs:line60 at System。IO.FileSystemWatcher.OnChanged(FileSystemEventArgs e)在f:\dd\NDP\fx\src\services\io\system\io\FileSystemWatcher.cs:line中822。IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32action,字符串名称)f: 系统上的\dd\NDP\fx\src\services\io\system\io\FileSystemWatcher.cs:line 773
。IO.FileSystemWatcher.CompletionStatusChanged(UInt32errorCode,UInt32 numBytes,NativeOverlapped*overlappedPointer)f: \dd\NDP\fx\src\services\io\system\io\FileSystemWatcher.cs:line 594
系统线程_IOCompletionCallback。PerformIOCompletionCallback(UInt32errorCode,UInt32 numBytes,NativeOverlapped*pOVERLAP)f: \dd\ndp\clr\src\BCL\system\threading\doverlapped.cs:line 121此异常最初是在此调用堆栈中引发的:系统__Error.cs中的IO.__Error.WinIOError(int,string)系统IO.FileStream.Init(字符串,System.IO.FileMode,System.IO.FileAccess,int,bool,System.IO_FileShare,int,系统IO.FileOptions,微软Win32.Win32Native.SECURITY_ATTRIBUTES,字符串,布尔,布尔,bool)在filestream.cs中系统FileStream.cs中的IO.FileStream.FileStream(字符串、System.IO.FileMode、System.IO.FileAccess、System.IO.VileShare)系统配置内部的InternalConfigHost。StaticOpenStreamForRead(字符串)在InternalConfigHost.cs中系统配置内部的InternalConfigHost。系统配置内部的IIinternalConfigHost。OpenStreamForRead(字符串)在InternalConfigHost.cs中系统配置ClientConfigurationHost。OpenStreamForRead(字符串)在ClientConfigurationHost.cs中系统配置UpdateConfigHost。UpdateConfigHost.cs中的OpenStreamForRead(字符串)系统配置隐含计算机配置主机。OpenStreamForRead(字符串)在ImplicitMachineConfigHost.cs中系统配置BaseConfigurationRecord。BaseConfigurationRecord.cs 中的InitConfigFromFile()
内部异常1:IOException:进程无法访问文件'C:\Source\Solutions\MySolution_1.2.3\MySolution\bin\Debug\MySolution.main.config'因为它正被另一个进程使用。
我试过用一个简单的锁,它不起作用。
这是我的包装的构造函数
public FileWatcher(string appConfigFilePath)
{
_appConfigFilePath = appConfigFilePath;
_lastRead = DateTime.MinValue;
var watcher = new FileSystemWatcher(Path.GetDirectoryName(_appConfigFilePath))
{
Filter = Path.GetFileName(_appConfigFilePath),
NotifyFilter = NotifyFilters.LastWrite,
EnableRaisingEvents = true
};
watcher.Changed += OnChanged;
watcher.Error += OnError;
}
有什么建议吗?
感谢
我不得不像log4net那样管理一些访问文件的重试。我建议你也这样做(github上有log4net源)