The process cannot access the file 'file path' because it is being used (unknown why)

44 Views Asked by At

reading this post IOException: The process cannot access the file 'file path' because it is being used by another process and following demonstrated its truth. By using Resource Monitor it showed that my dll is used by my program.

The question is maybe in detail here, because static variant works when its the only content in check_4_Update function and with all the stuff around it ends up with the exception.

Anyone able to explain/determine which function the block comes from ? I have an executable that uses a reference from a class that is created as dll so Class.Constructor is the Reference to this dll. Thank you in advance

static variant:

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; }

dynamic variant:

    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; }
    }

It looks like using System.Reflection.Assembly.... is causing it.

1

There are 1 best solutions below

1
beg-inner On

Solution:

this

string _fName = Path.GetFileNameWithoutExtension( check_Update_dll( _rPath + _uPath ) );

solves it.