如何为 Assembly.LoadFrom 方法进行卸载

How to make unload for Assembly.LoadFrom method

我正在使用 win CE 平台开发 C# 紧凑型应用程序,我正在使用此代码.....

var assembly = Assembly.LoadFrom(assemblyFile);
Version ver = assembly.GetName().Version;
theVsertion = ver.ToString();

获取文件版本但在另一种方法中,我必须删除旧文件,并且当我尝试使用此代码时。

        if (File.Exists(assemblyFile))
        {
            File.Delete(assemblyFile);
        }

如果发现这个错误

" process cannot access the file ### because it is being used by another process."

拜托,任何人都可以帮助我解决这个问题。

卸载程序集的唯一方法是卸载 AppDomain。 另一种方法是将文件读入 byte[] 并将此 byte[] 作为程序集加载。

仅作记录:这是问题的解决方案。 只需用此代码替换程序集 class。

public static Version GetFileVersionCe(string fileName)
{
    int handle = 0;
    int length = GetFileVersionInfoSize(fileName, ref handle);
    Version v = null;
    if (length > 0)
    {
        IntPtr buffer = System.Runtime.InteropServices.Marshal.AllocHGlobal(length);
        if (GetFileVersionInfo(fileName, handle, length, buffer))
        {
            IntPtr fixedbuffer = IntPtr.Zero;
            int fixedlen = 0;
            if (VerQueryValue(buffer, "\", ref fixedbuffer, ref fixedlen))
            {
                byte[] fixedversioninfo = new byte[fixedlen];
                System.Runtime.InteropServices.Marshal.Copy(fixedbuffer, fixedversioninfo, 0, fixedlen);
                v = new Version(
                    BitConverter.ToInt16(fixedversioninfo, 10), 
                    BitConverter.ToInt16(fixedversioninfo,  8), 
                    BitConverter.ToInt16(fixedversioninfo, 14),
                    BitConverter.ToInt16(fixedversioninfo, 12));
            }
        }
        Marshal.FreeHGlobal(buffer);
    }
    return v;
}

[DllImport("coredll", EntryPoint = "GetFileVersionInfo", SetLastError = true)]
private static extern bool GetFileVersionInfo(string filename, int handle, int len, IntPtr buffer);
[DllImport("coredll", EntryPoint = "GetFileVersionInfoSize", SetLastError = true)]
private static extern int GetFileVersionInfoSize(string filename, ref int handle);
[DllImport("coredll", EntryPoint = "VerQueryValue", SetLastError = true)]
private static extern bool VerQueryValue(IntPtr buffer, string subblock, ref IntPtr blockbuffer, ref int len);