我想向用戶顯示舊的和新的版本號,然後複製新版本。當我試圖在顯示版本信息後複製文件時,我得到以下例外處置程序集
進程無法訪問文件'C:\ Auto TEC \ Common.dll',因爲它正在被另一個進程使用。
下面是代碼:
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)
{
// 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);
}
}
}
如何卸載從對象的裝配信息?
重複?看看這個http://stackoverflow.com/questions/123391/how-to-unload-an-assembly-from-the-primary-appdomain – goenning 2010-09-30 15:53:14