目前正在使用如何检查装配

时间:2012-12-09 11:54:14

标签: c# installer vsto .net-assembly

我正在研究VSTO项目。在卸载时我想检测vsto项目的程序集是否正在使用?因为如果正在使用程序集,我想停止卸载操作。

我正在使用Installer类在卸载时执行我的代码。

3 个答案:

答案 0 :(得分:1)

在您的情况下,我认为您可以重复使用我之前为Excel VSTO插件项目编写的以下自定义函数。将它粘贴在您的Installer类中,并在Uninstall事件处理程序中调用它。

Process[] xlProc;
public bool isExcelRunning()
    {
        try
        {
            xlProc = Process.GetProcessesByName("excel");
            if (xlProc.Length > 0)
            {
                logEvents("A running instance of Excel has been detected. The installation is stopping.");
                return true;
            }
        }
        catch { }
        return false;
    }

希望它有所帮助。

答案 1 :(得分:0)

您可以使用此代码检查特定程序集是否已加载。

using System;
using System.Runtime.InteropServices;

namespace ConsoleTestAssembly
{
    class Program
    {
        static void Main()
        {
            string response;

            var exist = AssemblyExist("Camelot.SharePointConnector", out response);
            Console.WriteLine(string.Concat(exist, Environment.NewLine, response,  Environment.NewLine));

            exist = AssemblyExist("Camelot.SharePointIntegration", out response);
            Console.WriteLine(string.Concat(exist, Environment.NewLine, response, Environment.NewLine));

            exist = AssemblyExist("Camelot.NoExisting", out response);
            Console.WriteLine(string.Concat(exist, Environment.NewLine, response, Environment.NewLine));

            Console.ReadKey();
        }

        public static bool AssemblyExist(string assemblyname, out string response)
        {
            try
            {
                response = QueryAssemblyInfo(assemblyname);
                return true;
            }
            catch (System.IO.FileNotFoundException e)
            {
                response = e.Message;
                return false;
            }
        }

        // If assemblyName is not fully qualified, a random matching may be 
        public static String QueryAssemblyInfo(string assemblyName)
        {
            var assembyInfo = new AssemblyInfo {cchBuf = 512};
            assembyInfo.currentAssemblyPath = new String('', assembyInfo.cchBuf);

            IAssemblyCache assemblyCache;

            // Get IAssemblyCache pointer
            var hr = GacApi.CreateAssemblyCache(out assemblyCache, 0);
            if (hr == IntPtr.Zero)
            {
                hr = assemblyCache.QueryAssemblyInfo(1, assemblyName, ref assembyInfo);
                if (hr != IntPtr.Zero)
                {
                    Marshal.ThrowExceptionForHR(hr.ToInt32());
                }
             }
             else
             {
                 Marshal.ThrowExceptionForHR(hr.ToInt32());
             }

             return assembyInfo.currentAssemblyPath;
        }
    }

    internal class GacApi
    {
        [DllImport("fusion.dll")]
        internal static extern IntPtr CreateAssemblyCache(
            out IAssemblyCache ppAsmCache, int reserved);
    } 

    // GAC Interfaces - IAssemblyCache. As a sample, non used vtable entries 
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
    Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")]
    internal interface IAssemblyCache
    {
        int Dummy1();
        [PreserveSig()]
        IntPtr QueryAssemblyInfo(
            int flags,
            [MarshalAs(UnmanagedType.LPWStr)]
            String assemblyName,
            ref AssemblyInfo assemblyInfo);

        int Dummy2();
        int Dummy3();
        int Dummy4();
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct AssemblyInfo
    {
        public int cbAssemblyInfo;
        public int assemblyFlags;
        public long assemblySizeInKB;

        [MarshalAs(UnmanagedType.LPWStr)]
        public String currentAssemblyPath;

        public int cchBuf;
    }

}

答案 2 :(得分:0)

我过去常常确定进程/程序集是否正在运行:

使用System.Diagnostics.Process;

 Process[] processlist = Process.GetProcessesByName("Yourassemblyname");

 Process[] processlist = Process.GetProcesses();

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx