none
如何在選擇一個目錄之後,再選擇該目錄的目錄 RRS feed

  • 問題

  •  

    各位前輩,由於我的程式碼只能看一個目錄而已,而不能其他屬於該目錄底下的目錄,請問該如何改之

    這是屬於檔案io與資料存取秘訣的範例的第一章第16個範例,我順便附上我所寫的程式碼

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace readdir3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form_Load(object sender, EventArgs e)
            {
                DiskDrivebox.Items.AddRange(Directory.GetLogicalDrives());
            }

            private void DiskDriveSelected(object sender, EventArgs e)
            {
                if (DiskDrivebox.SelectedIndex != -1)
                {
                    try
                    {
                        string[] subdirectoryEntries = Directory.GetDirectories((string)DiskDrivebox.SelectedItem);
                        DirectoryBox.DataSource = subdirectoryEntries;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        DirectoryBox.DataSource = null;
                        txtResult.Clear();
                      
                    }
                }

            }

            private void DirectoryBoxSelected(object sender, EventArgs e)
            {
                if (DirectoryBox.SelectedIndex != -1)
                {
                    frmStatus frmStatusMessage = new frmStatus();
                    MessageBox.Show("嘗試取得所有層級的子目錄,處理中,請稍後....");

                    DirectoryInfo theDir = new DirectoryInfo((string)DirectoryBox.SelectedItem);
                    StringBuilder sb = new StringBuilder();

                    if (theDir.Exists)
                    {
                   //     sb.Append("此目錄的完整路徑是:");
                   //     sb.Append(theDir.FullName);
                    //    sb.Append(",它是 ");
                    //    sb.Append(theDir.Parent.Name);
                    //    sb.AppendLine(" 的子目錄。");

                     //   sb.Append("根目錄:");
                     //   sb.AppendLine(theDir.Root.Name);

                     //   sb.Append("屬性:");
                      //  sb.AppendLine(theDir.Attributes.ToString());

                    //    sb.Append("建立時間:");
                    //    sb.AppendLine(theDir.CreationTime.ToString());

                    //    sb.Append("最近一次的存取時間:");
                      //  sb.AppendLine(theDir.LastAccessTime.ToString());

                     //   sb.Append("最近一次的寫入時間:");
                     //   sb.AppendLine(theDir.LastWriteTime.ToString());

                        FileInfo[] entries;
                        sb.AppendLine("-----------------------------------");

                        try
                        {
                            entries = theDir.GetFiles("*", SearchOption.AllDirectories);
                        }
                        catch
                        {
                            sb.Append("你沒有權力閱覽");
                            txtResult.Text = sb.ToString();
                            return;
                        }
                        if (entries.Length > 0)
                        {
                          //  sb.AppendLine(theDir.FullName + " 之下的所有目錄層級中的檔案名稱");
                            sb.AppendLine("-------------------------------------------");

                            foreach (FileInfo entry in entries)
                            {
                                sb.Append(entry.Extension);
                                sb.AppendLine(entry.Length.ToString()+"位元組");
                            }
                        }
                        else
                        {
                            sb.Append("目錄不存在或是您沒有權限來檢視它。");
                        }

                        frmStatusMessage.Close();
                        txtResult.Text = sb.ToString();
                    }

               
                }

            }

            private void btnCreateFile(object sender, EventArgs e)
            {
                string myTextFilePath = @"c:\test1.txt";
                FileInfo fileInfo = new FileInfo(myTextFilePath);
                StreamWriter sw = f

    2008年3月30日 上午 11:25

所有回覆

  •  

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;

    namespace readdir3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form_Load(object sender, EventArgs e)
            {
                DiskDrivebox.Items.AddRange(Directory.GetLogicalDrives());
            }

            private void DiskDriveSelected(object sender, EventArgs e)
            {
                if (DiskDrivebox.SelectedIndex != -1)
                {
                    try
                    {
                        string[] subdirectoryEntries = Directory.GetDirectories((string)DiskDrivebox.SelectedItem);
                        DirectoryBox.DataSource = subdirectoryEntries;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        DirectoryBox.DataSource = null;
                        txtResult.Clear();
                      
                    }
                }

            }

            private void DirectoryBoxSelected(object sender, EventArgs e)
            {
                if (DirectoryBox.SelectedIndex != -1)
                {
                    frmStatus frmStatusMessage = new frmStatus();
                    MessageBox.Show("嘗試取得所有層級的子目錄,處理中,請稍後....");

                    DirectoryInfo theDir = new DirectoryInfo((string)DirectoryBox.SelectedItem);
                    StringBuilder sb = new StringBuilder();

                    if (theDir.Exists)
                    {
                   //     sb.Append("此目錄的完整路徑是:");
                   //     sb.Append(theDir.FullName);
                    //    sb.Append(",它是 ");
                    //    sb.Append(theDir.Parent.Name);
                    //    sb.AppendLine(" 的子目錄。");

                     //   sb.Append("根目錄:");
                     //   sb.AppendLine(theDir.Root.Name);

                     //   sb.Append("屬性:");
                      //  sb.AppendLine(theDir.Attributes.ToString());

                    //    sb.Append("建立時間:");
                    //    sb.AppendLine(theDir.CreationTime.ToString());

                    //    sb.Append("最近一次的存取時間:");
                      //  sb.AppendLine(theDir.LastAccessTime.ToString());

                     //   sb.Append("最近一次的寫入時間:");
                     //   sb.AppendLine(theDir.LastWriteTime.ToString());

                        FileInfo[] entries;
                        sb.AppendLine("-----------------------------------");

                        try
                        {
                            entries = theDir.GetFiles("*", SearchOption.AllDirectories);
                        }
                        catch
                        {
                            sb.Append("你沒有權力閱覽");
                            txtResult.Text = sb.ToString();
                            return;
                        }
                        if (entries.Length > 0)
                        {
                          //  sb.AppendLine(theDir.FullName + " 之下的所有目錄層級中的檔案名稱");
                            sb.AppendLine("-------------------------------------------");

                            foreach (FileInfo entry in entries)
                            {
                                sb.Append(entry.Extension);
                                sb.AppendLine(entry.Length.ToString()+"位元組");
                            }
                        }
                        else
                        {
                            sb.Append("目錄不存在或是您沒有權限來檢視它。");
                        }

                        frmStatusMessage.Close();
                        txtResult.Text = sb.ToString();
                    }

               
                }

            }

            private void btnCreateFile(object sender, EventArgs e)
            {
                string myTextFilePath = @"c:\test1.txt";
                FileInfo fileInfo = new FileInfo(myTextFilePath);
                StreamWriter sw = fileInfo.CreateText();
                sw.Write(txtResult.Text + "\r\n");
                sw.Flush();
              //  MessageBox.Show("已寫入" + txtResult.Text);
            }

        
        }
    }對不起,這才是我寫的完整程式碼

    2008年3月30日 上午 11:27