简体   繁体   English

关闭c#应用程序后删除DLL

[英]Delete DLL after closing c# application

I Have ac# application that include various esternal DLL.我有包含各种 esternal DLL 的 ac# 应用程序。 When the application start, dlls are exctracted in the .exe folder to grant the correct execution.应用程序启动时,会在 .exe 文件夹中提取 dll 以确保正确执行。 here the code:这里的代码:

var executingAssembly = Assembly.GetExecutingAssembly();

string folderName = string.Format("{0}.Resources.DLLs", executingAssembly.GetName().Name);
var list = executingAssembly
.GetManifestResourceNames()
.ToArray();
foreach (var item in list)
{
    File.WriteAllBytes(item.Replace("myapp.DLLs.", ""), 
    ReadAllBytes(executingAssembly.GetManifestResourceStream(item)));
}

when i close the form, i want to delete those files with this code, associated to the form closing event:当我关闭表单时,我想删除这些与表单关闭事件相关联的代码的文件:

private void CleanFiles(Object sender, FormClosingEventArgs e)
{
    var executingAssembly = Assembly.GetExecutingAssembly();
    string folderName = string.Format("{0}.Resources.DLLs", executingAssembly.GetName().Name);
    string folder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
    var list = executingAssembly
    .GetManifestResourceNames()
    .ToArray();
    foreach (var item in list)
    {

        File.Delete(folder + @"\" + item.Replace("myapp.DLLs.", ""));
    }
}

If I open and then close the form, it works perfectly.如果我打开然后关闭表单,它就可以完美运行。 But, if I open the form, do some operations, it throw an exception during the closing operations because access to dlls is denied.但是,如果我打开表单,做一些操作,它会在关闭操作期间抛出异常,因为对 dll 的访问被拒绝。 How can I release all dlls/resources?如何释放所有 dll/资源?

I assume that if you copy these DLLs, you load and use them afterwards.我假设如果您复制这些 DLL,您会在之后加载并使用它们。

There's a slight issue with dynamically loading DLLs, being that you can't just unload them.动态加载 DLL 有一个小问题,因为您不能只是卸载它们。 Once loaded into your application domain, they're here to stay.一旦加载到您的应用程序域中,它们就会留下来。

The solution is thus to simply create a new application domain, load the DLL inside of it, and when you're done, unload this new application domain.因此,解决方案是简单地创建一个新的应用程序域,在其中加载 DLL,完成后,卸载这个新的应用程序域。

Something like:就像是:

var dynamicDomain = AppDomain.CreateDomain("YourDomainName");
var dynamicallyLoadedAssembly = dynamicDomain.Load("YourAssembly");

// do stuff with your dynamically loaded assembly

AppDomain.Unload(dynamicDomain);

For more information on the topic: MSDN "How to: Load and unload assemblies" .有关该主题的更多信息: MSDN“如何:加载和卸载程序集”

You can (and should) implement exception handling around the parts where you manipulate files on the system:您可以(并且应该)围绕您在系统上操作文件的部分实施异常处理:

try
{
  // Manipulate your files here.
}
catch (Exception ex)
{
  // Handle exceptions here. you can also delete the dlls here if you wish.
  // Remember to check in CleanFiles if the files are already deleted.
}
finally
{
  // You could also get rid of the files here.
  // Finally-block is executed regardless if an exception was thrown or not.
}

Usefull links: Microsoft: Exception Handling , Microsoft: Best Ractices for exceptions有用的链接: 微软:异常处理微软:异常的最佳实践

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM