歡迎光臨 pure C# 技術社群. 登入註冊

pure C# 技術社群

- Microsoft .NET Framework 相關技術非官方討論社群 -

求如何看一個檔案中的所有檔案

  • 1
  • [第1/1頁 共6項]
#1

求如何看一個檔案中的所有檔案

  • a
 0.0 (0 人評價)

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 readdir4
{
    public partial class Form1 : Form
    {
        private const string DUMMY = "DUMMY";
     
        private enum ItemType
        {
            Directory = 1,
            File = 2
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Load_Form(object sender, EventArgs e)
        {
            LoadTreeView();
        }

        private void LoadTreeView()
        {
            string[] myDrives = Directory.GetLogicalDrives();

            tvwRoot.Nodes.Clear();

            foreach (string strDrive in myDrives)
            {
                TreeNode myTreeNode = new TreeNode();

                myTreeNode.Text = strDrive;
                myTreeNode.Nodes.Add(DUMMY);

                tvwRoot.Nodes.Add(myTreeNode);
            }
        }

        private void tvwRoot_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                switch ((ItemType)e.Node.Tag)
                {
                    case ItemType.File:
            
                        FileInfo fi = new FileInfo(e.Node.FullPath);
                     
                        lblLength.Text = fi.Length.ToString();
                        DisplayFSIProperties(fi);
                        break;

                    case ItemType.Directory:
                      
                        lblLength.Text = String.Empty;
                        DirectoryInfo di = new DirectoryInfo(e.Node.FullPath);
            
                        break;
                    default:
                     
                        ClearProperties();
                        break;
                }
            }
            catch (Exception)
            {
               
                // MessageBox.Show(exp.Message, this.Text);
            }
        }

        private void vwRoot_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            try
            {
                e.Node.Nodes.Clear();

                // 呼叫 AddFolders 程序來替節點加入目錄名稱。
                AddFolders(e.Node);

                // 呼叫 AddFiles 程序來替節點加入檔案名稱。
                AddFiles(e.Node);
            }
            catch (IOException exp)  // 通常發生於磁碟裝置尚未就緒時。
            {
                MessageBox.Show(exp.Message);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString(), this.Text);
            }
        }

        private void AddFolders(TreeNode nod)
        {
            string strPath = nod.FullPath;
            string[] myDirectories = Directory.GetDirectories(strPath);

            foreach (string strDir in myDirectories)
            {
                TreeNode myTreeNode = new TreeNode();

                myTreeNode.Text = Path.GetFileName(strDir);
                myTreeNode.Tag = ItemType.Directory;
                myTreeNode.Nodes.Add(DUMMY);

                nod.Nodes.Add(myTreeNode);
            }
        }

        // 此程序用來替節點加入檔案名稱。
        public void AddFiles(TreeNode nod)
        {
            string strPath = nod.FullPath;
            string[] myFiles = Directory.GetFiles(strPath);

            foreach (string strFile in myFiles)
            {
                TreeNode myTreeNode = new TreeNode();

                myTreeNode.Text = Path.GetFileName(strFile);
                myTreeNode.Tag = ItemType.File;

                nod.Nodes.Add(myTreeNode);
            }

        }

        public void DisplayFSIProperties(FileSystemInfo fsi)
        {
       
          
                lblExtension.Text = fsi.Extension;
           
        }
        private void ClearProperties()
        {
          
          
                lblLength.Text = String.Empty;
           
       
        }
     

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


    }
}
各位前輩,我這個程式只能看檔案夾中的某個檔案,若我想要看所有的檔案該如何改


  • 回覆
  • |
  • 引用
  • |
  • 編輯
  • |
  • 私人訊息


贊助商連結

#2

Re: 求如何看一個檔案中的所有檔案

  • a
 0.0 (0 人評價)
取得c:\test\目錄下的檔案清單(字串陣列)
string targetDirectory = @"c:\test\";
string [] fileEntries = Directory.GetFiles(targetDirectory);


取得c:\test\目錄下的.txt檔案清單(字串陣列)
string targetDirectory = @"c:\test\";
string [] fileEntries = Directory.GetFiles(targetDirectory, ".txt");


取得c:\test\目錄下的檔案清單(FileInfo陣列)
string targetDirectory = @"c:\test\";
DirectoryInfo dir = new DirectoryInfo(targetDirectory );
FileInfo[] files= dir.GetFiles();


取得c:\test\目錄下的.txt檔案清單(FileInfo陣列)
string targetDirectory = @"c:\test\";
DirectoryInfo dir = new DirectoryInfo(targetDirectory, ".txt");
FileInfo[] files= dir.GetFiles();


  • 回覆
  • |
  • 引用
  • |
  • 編輯
  • |
  • 私人訊息

#3

Re: 求如何看一個檔案中的所有檔案

  • a
 0.0 (0 人評價)
這位前輩所寫的應該要放在那裡,我還是有一點看不懂


  • 回覆
  • |
  • 引用
  • |
  • 編輯
  • |
  • 私人訊息

#4

Re: 求如何看一個檔案中的所有檔案

  • a
 0.0 (0 人評價)
對不起,諸位前輩們,我所要問的是當我開啟一個資料夾後,原本我下面的顯示一個檔案的副檔名和大小,但我想要把我所開啟的資料中的所有檔案的副檔名和大小都顯示出來.懇請諸位前輩們的幫忙

  • 回覆
  • |
  • 引用
  • |
  • 編輯
  • |
  • 私人訊息

#5

Re: 求如何看一個檔案中的所有檔案

  • a
 0.0 (0 人評價)
我還是認為我上面以經回答了你的問題
但你好像還是不明白
所以你可能要描述清楚到底是哪邊不懂
不然別人很難去回答問題


  • 回覆
  • |
  • 引用
  • |
  • 編輯
  • |
  • 私人訊息

#6

Re: 求如何看一個檔案中的所有檔案

  • a
 0.0 (0 人評價)
前輩,我想問一下,那四個你所寫的的片段,若是要看所有的檔案是否為@"*"

  • 回覆
  • |
  • 引用
  • |
  • 編輯
  • |
  • 私人訊息
  • 1
  • [第1/1頁 共6項]