日期:2014-05-20  浏览次数:20896 次

如何在 C# 程序中得到 GAC 中的内容
请问如何在 C# 程序中得到全局程序集缓存(Global Assembly Cache)中的内容?
也就是要在 C# 程序中获得 gacutil /l 命令的内容。

C# code

using System.Reflection;

public static class Pub
{
  public static Assembly[] GetGAC()
  {
    Assebmly[] gac;
    // TODO ...
    return gac;
  }
}



也就是要完成上述程序。

谢谢!

------解决方案--------------------
以前写的在办公室电脑了 网上随便找了个看看行不
C# code

 // 实例一个Process类,启动一个独立进程
            Process p = new Process();

            // 设定程序名
            p.StartInfo.FileName = "cmd.exe";
            // 关闭Shell的使用
            p.StartInfo.UseShellExecute = false;
            // 重定向标准输入
            p.StartInfo.RedirectStandardInput = true;
            // 重定向标准输出
            p.StartInfo.RedirectStandardOutput = true;
            //重定向错误输出
            p.StartInfo.RedirectStandardError = true;
            // 设置不显示窗口
            p.StartInfo.CreateNoWindow = true;

            // 启动进程
            string pingrst;

            p.Start();

            p.StandardInput.WriteLine("ping -n 1 " + strIp);
            p.StandardInput.WriteLine("exit");

            // 从输出流获取命令执行结果
            string strRst = p.StandardOutput.ReadToEnd();

------解决方案--------------------
模拟cmd命令,先获取GAC里dll文件,再通过反射路径下的DLL获取相关信息
cd c:\windows\accembly\GAC_MSIL
xcopy *.* c:\temp\ /e


------解决方案--------------------
试试吧,就是效率不高:
C# code

        public static Assembly[] GetGAC()
        {
            List<Assembly> list = new List<Assembly>();
            foreach (string s in Directory.GetFiles(@"C:\Windows\assembly", "*.*", SearchOption.AllDirectories))
            {
                try
                {
                    Assembly ass = Assembly.LoadFrom(s);
                    if (ass != null)
                        list.Add(ass);
                }
                catch
                {
                    continue;
                }
            }
            return list.ToArray();
        }

------解决方案--------------------
msdn

Knowledge Base
id=317540

DOC: Global Assembly Cache (GAC) APIs Are Not Documented in the .NET Framework Software Development Kit (SDK) Documentation