0

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

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

4

3 回答 3

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

希望它有所帮助。

于 2012-12-11T15:56:35.633 回答
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;
    }

}
于 2012-12-09T13:57:33.237 回答
0

我习惯于跟踪以确定进程/程序集是否正在运行:

使用 System.Diagnostics.Process;

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

或者

 Process[] processlist = Process.GetProcesses();

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

于 2012-12-09T16:18:41.077 回答