I am trying to show the version numbers of both old and new to user then copy the new version. When I was trying copy the file after showing the version information I am getting the following exception
The process cannot access the file 'C:\Auto TEC\Common.dll' because it is being used by another process.
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Diagnostics;
namespace copyfile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sourceDirectory = @"E:\newversion\Auto TEC";
string targetDirectory = @"C:\Auto TEC";
Copy(sourceDirectory, targetDirectory);
label3.Text = "sucess";
loadAssembyNames();
}
void f2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Close();
}
public static void Copy(string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
开发者_如何学C {
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it's new directory.
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
string _errMsg;
//private AssemblyInformation _info;
private void getfilenames(string directoryPath, int location)
{
string[] path = new string[25];
int count = 0;
foreach (string file in System.IO.Directory.GetFiles(directoryPath))
{
path[count] = file;
count++;
}
Assembly asm = null;
for (int i = 0; i < count; i++)
{
try
{
//asm = Assembly.LoadFrom(path[i]);
asm = Assembly.LoadFile(path[i]);
if (asm != null)
{
if (location == 1)
{
listBox1.Items.Add(asm.GetName().Name + asm.GetName().Version.ToString());
}
else
{
listBox2.Items.Add(asm.GetName().Name + asm.GetName().Version.ToString());
}
}
}
catch (Exception err)
{
this._errMsg = err.Message;
}
}
asm = null;
GC.Collect();
}
private void Form1_Load(object sender, EventArgs e)
{
loadAssembyNames();
}
private void loadAssembyNames()
{
listBox1.Items.Clear();
listBox2.Items.Clear();
getfilenames(@"C:\Auto TEC", 1);
getfilenames(@"E:\newversion\Auto TEC", 2);
}
}
}
How do I unload the assembly information from object?
There's no way to unload an individual assembly without unloading all of the appdomains containing it. So you should use different appdomain foreach of your assemblies.
More info :
- Unloading an assembly
- Assembly Unload? Use AppDomain.Unload instead
Once assembly is loaded it cannot be unloaded.
You have one option to copy the bytes to the memory and then load using Assembly.Load(byte[]).
Also you may use FileVersionInfo
which is much easier.
You can't unload an assembly once it's loaded into your AppDomain. The only way to accomplish this is to load the assembly in a separate AppDomain, and unload the entire AppDomain.
As soon as you reference an assembly or type from that assembly within your AppDomain, it will be loaded into that AppDomain and not released.
In .Net, if you load the assembly in the same AppDomain as the application, then it's not possible to unload it (this includes loading it for reflection only). If you need to inspect the assembly internals, I recommend using Mono.Cecil (http://www.mono-project.com/Cecil).
I think i am not in time to answer this questions but i had the same problem and for future reference you just need to use "FileVersionInfo.GetVersionInfo"
system.Diagnostics
and thats it, this option will not load the assembly just its information and you will be able to replace the file.
精彩评论