如何快速獲得.net程序引用的程序集

如何快速獲得.net程序引用的程序集

 

在配置管理和測試管理分工明確的公司,偶爾會拿到一個測試版本,測試時這也問題那也問題,經常性的崩潰。這時,您可能需要考慮是否版本缺少文件的可能性了。有什麼辦法能夠理直氣壯的對配置人員或者開發人員說,你給的版本少文件,並且少哪個文件呢?那這裏小羅告訴你答案。

   .net程序是由程序集組成,程序集分爲公有程序集和私有程序集,公有程序集一般需要在C:/WINDOWS/assembly中進行強命名註冊,私有程序集一般會放在可執行exe相同的路徑下面、或者是可執行exe程序的config文件中的路徑位置。(有關.net基本知識請參考相關文檔,你只需要知道這麼多就可以測試了)。

 

具體步驟:

1,在vs2005中新建一個工程,如testtools,項目類型選擇windows應用程序。

 

2,在windows form上面拖入一個文件瀏覽的botton,和一個文本框,然後在botton的事件上面錄入如下代碼:

 

 /// <summary>
        /// 瀏覽按鈕
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter="可執行文件|*.exe";
            this.openFileDialog1.ShowDialog();
            this.textBox1.Text = this.openFileDialog1.FileName.Trim();
            if (this.textBox1.Text.Trim() == string.Empty)
            {
                MessageBox.Show("選擇文件,文件類型爲dll或者exe", "數慧測試平臺");
                return;

            }
           return;

        }

 

3,在windows form上面拖入普通的botton,然後在botton的事件上面錄入如下代碼:

 

       /// <summary>
        /// 獲取引用列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Trim() == string.Empty)
            {
                MessageBox.Show("請先選擇文件", "數慧測試平臺");
                return;
            }

          
            if (this.radioButton1.Checked == true)
            {
             //這裏將程序引用信息寫入到txt中,讀者可以自定義。
                TestLogManager testlog = new TestLogManager();
                StreamWriter swriter = testlog.getStreamwriter(Application.StartupPath + "//安裝測試//" + "程序引用日誌.txt");
                swriter.WriteLine("引用dll名稱/t" + "版本/t" + "公鑰");
               
               
                IList<string> pList = new List<string>();
                ArrayList pList2 = new ArrayList();
                try
                {
                    Assembly asscembly = Assembly.LoadFile(this.textBox1.Text.Trim());//裝載程序集
                    GetAssemblysRecurse(asscembly, pList, pList2);
                    //對輸出結果進行排序
                    pList2.Sort(new myReverserClass());

                    foreach (AssemblyName pAssemblyNameReferenced in pList2)
                    {

                        // 獲得程序引用的名稱
                        swriter.WriteLine("" + pAssemblyNameReferenced.Name + "/t" + pAssemblyNameReferenced.Version + "/t" + pAssemblyNameReferenced.KeyPair);

                    }
                }
                finally
                {
                    swriter.Close();
                }
                MessageBox.Show("完成!" + "請查看:" + Application.StartupPath + "//安裝測試//程序引用日誌.txt", "數慧測試平臺");
                return;

            }
            else
            {
                return;
            }

        }
        /// <summary>
        /// 排序方法
        /// </summary>
        public class myReverserClass : IComparer
        {
            int IComparer.Compare(object x, object y)
            {
                return (x as AssemblyName).Name.CompareTo((y as AssemblyName).Name);
            }
        }

        /// <summary>
        /// 遞歸函數,遞歸獲得程序集合名稱,然後寫入日誌中。
        /// </summary>
        /// <param name="swriter"></param>
        /// <param name="asscembly"></param>
        /// <param name="pList"></param>
        private void GetAssemblysRecurse(Assembly asscembly, IList<string> pList, ArrayList pList2)
        {
            AssemblyName[] assemblyname = asscembly.GetReferencedAssemblies();//獲得引用程序集
            foreach (AssemblyName pAssemblyNameReferenced in assemblyname)
            {
                Assembly pAssemblyReferenced = null;
                //先從公共的Assembly中檢查是否存在相應的dll,如:C:/WINDOWS/assembly
                try
                {
                    pAssemblyReferenced = Assembly.Load(pAssemblyNameReferenced);
                }
                catch (Exception)
                {
                }
                //從可執行的exe下面檢查是否存在相應的dll
                if (pAssemblyReferenced == null)
                {
                    try
                    {
                       
                        pAssemblyReferenced = Assembly.LoadFile(Path.GetDirectoryName(this.textBox1.Text.Trim()) +
                            Path.DirectorySeparatorChar + pAssemblyNameReferenced.Name + ".dll");
                    }
                    catch (Exception)
                    {

                    }
                }
                //如果存在引用的dll,然後判斷是否爲系統的dll。
                if (pAssemblyReferenced != null)
                {
                    if (pAssemblyNameReferenced.Name.IndexOf("Test_ms") > -1 ||
                        pAssemblyNameReferenced.Name.IndexOf("Test_System") > -1||
                        pAssemblyNameReferenced.Name.IndexOf("test_office") > -1||
                        pAssemblyNameReferenced.Name.IndexOf("test_Interop") > -1)
                    {
                        continue;
                    }

                    //如果plist中不存在,則添加。
                    if (pList.IndexOf(pAssemblyNameReferenced.Name) == -1)
                    {
                        pList.Add(pAssemblyNameReferenced.Name);                                    
                       //將程序集添加到plist2中,plist2主要是用來排序。
                        pList2.Add(pAssemblyNameReferenced);
                        //執行遞歸方法
                        GetAssemblysRecurse(pAssemblyReferenced, pList, pList2);
                    }
                }

            }
        }

 

4,編譯工程,然後執行exe即可獲得引用的程序集列表。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章