在卸载AppDomain后尝试删除COM dll文件时获取异常

时间:2017-09-21 06:30:45

标签: c# dll .net-assembly appdomain appdomainsetup

在卸载AppDomain后尝试删除COM dll文件时获取异常。 异常详细信息:无法应用自动更新 System.UnauthorizedAccessException:拒绝访问路径“IQVdxp32.dll”。 代码:

private bool ApplyUpdate(string tempfolderpath)
    {
        bool result;
        try
        {
            Destroy();
            if (Directory.Exists(moduledir))
                Directory.Delete(moduledir, true); /*Getting excpetion while deleting that unload appdomain directory*/
            DirectoryUtils.Copy(tempfolderpath, moduledir);
            Create();
            log.Info("Auto update successful for module : " + modulename);
            result = true;
        }
        catch (Exception exception)
        {
            log.Error("Failed to apply auto update", exception);
            result = false;
        }
        return result;
    }
 public void Destroy()
    {
        if (appdomain != null)
        {
            lock (objappdomainlocker)
            {
                if (appdomain != null)
                {
                    CoFreeUnusedLibrariesEx(10000, (UInt32)0);
                    System.Threading.Thread.Sleep((int)10000);
                    if (loader != null && apolomain != null)
                    {
                        loader.CallInternalOnObject("Destroy", null, apolomain);
                    }
                    // unload the AppDomain completely
                    AppDomain.Unload(appdomain);
                    appdomain = null;
                    loader = null;
                    apolomain = null;
                    IsPersist = false;
                 }
            }
        }
    }

[DllImport("ole32.dll")]
    public static extern void CoFreeUnusedLibrariesEx(UInt32 unloadDelay, UInt32 reserved);

0 个答案:

没有答案