0

I think this problem seems a lot to be questioned, but I still haven't found a solution. I want to update my application by comparing dll. file version. I already got the file version, but when I want to update it, it gives me this error.

the code

FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(@"D:\Projects\Sample\bin\Debug\Sample.dll");
FileVersionInfo myFileVersionInfo2 = FileVersionInfo.GetVersionInfo(@"C:\Projects\Sample\bin\Debug\Sample.dll");
MessageBox.Show("Version number this: " + myFileVersionInfo.FileVersion);
MessageBox.Show("Version number 2: " + myFileVersionInfo2.FileVersion);

if (myFileVersionInfo.FileVersion != myFileVersionInfo2.FileVersion)
{
     string file = "Sample.dll";
     string source = @"D:\Projects\Sample\MainSystem\bin\Debug\";
     string target = Application.StartupPath;//@C:\
     string sourceFile = System.IO.Path.Combine(source, file);
     string destFile = System.IO.Path.Combine(target, file);
     System.IO.File.Copy(sourceFile, destFile, true); //<< error
{

the code run when I open the application and click button

the Error

System.IO.IOException: The process cannot access the file 'C:\Projects\Sample\bin\Debug\Sample.dll' because it is being used by another process.

Mittal Patel
  • 2,732
  • 14
  • 23
orlandeu man
  • 220
  • 1
  • 3
  • 14

1 Answers1

0

The Class FileVersionInfo 'locks' the dll from which you are reading the fileversion. In order to access it again, try

    myFileVersionInfo2 = default(FileVersionInfo);
Raphnika
  • 62
  • 9
  • The `FileVersionInfo` class uses the `GetFileVersionInfo` API, which doesn't indicate any locking nor have any corresponding `Release` or `Free` method. In addition, setting a variable to `null` has no impact here if the code is already past the last point at which the variable is read from - the corresponding object would already be eligible for GC. – Damien_The_Unbeliever Jan 24 '18 at 07:45