阅读这篇文章IOException:该进程无法访问文件“文件路径”,因为它正在被另一个进程使用并且以下内容证明了其真相。通过使用资源监视器,它显示我的 dll 被我的程序使用。
问题可能在这里很详细,因为当静态变体是 check_4_Update 函数中的唯一内容并且其周围的所有内容都以异常结束时,静态变体才会起作用。
有人能够解释/确定该块来自哪个函数吗? 我有一个可执行文件,它使用创建为 dll 的类的引用,因此 Class.Constructor 是对此 dll 的引用。 先谢谢你了
静态变体:
string _Source = @"path1\file.ext";
string _Target = @"path2\file.ext";
if ( File.Exists( _Source ) ) {
try { File.Copy(_Source,_Target,true); return true; }
catch (Exception e) { MessageBox.Show(e.Message); return false; }
动态变体:
private static string getBackName( string inStr, [System.Runtime.CompilerServices.CallerMemberName] string _mN = "" ) {
return _mN.Split('_').Last();
}
private static Int64 getFileVersion( string versionOfFile ) {
if ( !File.Exists( versionOfFile )) return 0;
return Int64.Parse( FileVersionInfo.GetVersionInfo( versionOfFile ).FileVersion.ToString().Replace(".","") );
}
private static string check_Update_dll( string _completePath ) {
string _fExt = "."+getBackName("");
string[] files = Directory.GetFiles( _completePath , "*"+_fExt );
switch ( files.Length ) {
case 0 : return null;
case 1 : return files[files.Length-1];
default : return null;
}
}
private static bool check_4_Update() {
string _fName = (System.Reflection.Assembly.GetEntryAssembly().GetName().Name).Replace( new Class.Constructor().GetType().Name , "");
string _uPath = "\\"+getBackName("")+"\\";
string _rPath = Path.GetDirectoryName( Application.ExecutablePath );
string _fExt = Path.GetExtension( check_Update_dll( _rPath + _uPath ) );
if ( _fExt == null ) return false;
string _Source = _rPath + _uPath + _fName + _fExt;
Int64 _uFVer = getFileVersion( _Source );
string _Target = _rPath + "\\" + _fName + _fExt;
Int64 _oFVer = getFileVersion( _Target );
if ( _oFVer >= _uFVer ) return false;
if ( File.Exists( _Source ) ) {
try { File.Copy(_Source,_Target,true); return true; }
catch (Exception e) { MessageBox.Show(e.Message); return false; }
} else { return false; }
}
看起来是使用 System.Reflection.Assembly.... 导致的。
解决方案:
using string _fName = Path.GetFileNameWithoutExtension( check_Update_dll( _rPath + _uPath ) );
解决了。