gpt4 book ai didi

c# - 处理组件

转载 作者:太空宇宙 更新时间:2023-11-03 18:46:40 25 4
gpt4 key购买 nike

我试图向用户显示新旧版本号,然后复制新版本。当我在显示版本信息后尝试复制文件时,出现以下异常

The process cannot access the file 'C:\Auto TEC\Common.dll' because it is being used by another process.

代码如下:

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

如何从对象中卸载程序集信息?

最佳答案

如果不卸载包含它的所有应用程序域,就无法卸载单个程序集。因此,您应该为每个程序集使用不同的应用程序域。

更多信息:

关于c# - 处理组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3832351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com