正在使用文件复制过程,

时间:2018-01-24 06:31:39

标签: c# winforms

我认为这个问题似乎有很多值得质疑,但我仍然没有找到解决方案。 我想通过比较dll.文件版本来更新我的应用程序。我已经有了文件版本,但是当我想要更新它时,它会给我this错误。

代码

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
{

我打开应用程序并单击按钮

时运行代码

错误

  

System.IO.IOException:进程无法访问文件&#39; C:\ Projects \ Sample \ bin \ Debug \ Sample.dll&#39;因为它正被另一个进程使用。

1 个答案:

答案 0 :(得分:0)

Class FileVersionInfo'锁定'你正在读取fileversion的dll。要再次访问它,请尝试

    myFileVersionInfo2 = default(FileVersionInfo);