[C#] 讀取目錄下的組件資訊並且可匯出組件名稱

  • 5594
  • 0
  • 2009-11-23

摘要:讀取目錄下的組件資訊並且可匯出組件名稱

 

只要指定某個資料夾,自動去讀取該資料下的(*.dll)檔,並且設定給GridView顯示,也可匯出組件名稱

應用程式版面配置如下:

版面配置.bmp

程式碼如下:

using System;
       using System.Collections.Generic;
       using System.ComponentModel;
       using System.Data;
       using System.Drawing;
       using System.Linq;
       using System.Text;
       using System.Windows.Forms;
        
       using System.IO;
       using System.Collections;
     
       namespace ReadAssemblyName
    
           public partial class Form1 : Form
           {        
               FolderBrowserDialog FFolderBrowserDialog = null;
               ArrayList FFileList = null;
               SaveFileDialog FSaveFileDialog = null;
               string[] sFiles;
        
               public Form1()
               {
                   InitializeComponent();
               }
        
               private void Form1_Load(object sender, EventArgs e)
               {
                   btnSearch.Enabled = false;
                   btnExport.Enabled = false;
                   dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
               }
        
               private void btnOpen_Click(object sender, EventArgs e)
               {
                   using (FFolderBrowserDialog = new FolderBrowserDialog())
                   {
                       if (FFolderBrowserDialog.ShowDialog(this) == DialogResult.OK)
                           txtPath.Text = FFolderBrowserDialog.SelectedPath;
                   }          
               }
        
               private void txtPath_TextChanged(object sender, EventArgs e)
               {
                   if (string.IsNullOrEmpty(((TextBox)(sender)).Text) == false)
                   {
                       btnSearch.Enabled = true;
                   }
               }
        
               private void btnSearch_Click(object sender, EventArgs e)
               {
                   string sFolderPath = txtPath.Text;
                   FFileList = new ArrayList();
                   FileInfo oFileInfo;
        
                   if (Directory.Exists(sFolderPath))
                   {
                       sFiles = Directory.GetFiles(sFolderPath, "*.dll");
        
                       foreach (string item in sFiles)
                       {
                           oFileInfo = new FileInfo(item);
                           FFileList.Add(oFileInfo);
                       }
        
                       dataGridView1.DataSource = FFileList;
                   }           
                   
               }
        
               /// <summary>
               /// dataGridView 的資料繫結完成事件。
               /// </summary>
               private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
               {
                   if (dataGridView1.RowCount > 0)//判斷 dataGridView 控制項是否有任何一筆資料。
                       btnExport.Enabled = true;
                   else
                       btnExport.Enabled = false;
               }
        
               /// <summary>
               /// 輸出組件檔名。
               /// </summary>        
               private void btnExport_Click(object sender, EventArgs e)
               {
                   using (FSaveFileDialog = new SaveFileDialog())
                   {
                       try
                       {
                           FSaveFileDialog.Filter = "(文件.txt)|*.txt";
                           if (FSaveFileDialog.ShowDialog() == DialogResult.OK)
                           {
                               using (StreamWriter oStreamWriter = new StreamWriter(FSaveFileDialog.FileName))
                               {
                                   foreach (FileInfo item in FFileList)
                                   {
                                       oStreamWriter.WriteLine(item.Name, ToString());
                                   }
                               }
        
                               MessageBox.Show("儲存成功!");
                           }
                       }
                       catch
                       {
                           MessageBox.Show("儲存失敗!");
                       }
                   }
               }                       
           }
       }

(ReadAssemblyName)

三小俠  小弟獻醜,歡迎指教