HOW TO:建立目錄清單

下列程式碼範例將示範如何使用 I/O 類別來建立目錄中具有副檔名 ".exe" 的所有檔案之清單。

範例

Imports System
Imports System.IO

Public Class DirectoryLister
    Public Shared Sub Main(args() As String)
        Dim path As String = Environment.CurrentDirectory
        If args.Length > 0 Then
            If Directory.Exists(args(0)) Then
                path = args(0)
            Else
                Console.WriteLine("{0} not found; using current directory:",
                    args(0))
            End If
        End If
        Dim dir As New DirectoryInfo(path)
        For Each f As FileInfo In dir.GetFiles("*.exe")
            Dim name As String = f.Name
            Dim size As Long = f.Length
            Dim creationTime As DateTime = f.CreationTime
            Console.WriteLine("{0,-12:N0} {1,-20:g} {2}", size,
                creationTime, name)
        Next f
    End Sub
End Class
using System;
using System.IO;

public class DirectoryLister
{
    public static void Main(String[] args)
    {
        string path = Environment.CurrentDirectory;
        if (args.Length > 0)
        {
            if (Directory.Exists(args[0]))
            {
                path = args[0];
            }
            else
            {
                Console.WriteLine("{0} not found; using current directory:",
                    args[0]);
            }
        }
        DirectoryInfo dir = new DirectoryInfo(path);
        foreach (FileInfo f in dir.GetFiles("*.exe"))
        {
            string name = f.Name;
            long size = f.Length;
            DateTime creationTime = f.CreationTime;
            Console.WriteLine("{0,-12:N0} {1,-20:g} {2}", size,
                creationTime, name);
        }
    }
}
using namespace System;
using namespace System::IO;

public ref class DirectoryLister
{
public:
    static void Main(array<String^>^ args)
    {
        String^ path = Environment::CurrentDirectory;
        if (args->Length > 0)
        {
            if (Directory::Exists(args[0]))
            {
                path = args[0];
            }
            else
            {
                Console::WriteLine("{0} not found; using current directory:",
                    args[0]);
            }
        }
        DirectoryInfo^ dir = gcnew DirectoryInfo(path);
        for each (FileInfo^ f in dir->GetFiles("*.exe"))
        {
            String^ name = f->Name;
            long size = f->Length;
            DateTime^ creationTime = f->CreationTime;
            Console::WriteLine("{0,-12:N0} {1,-20:g} {2}", size,
                creationTime, name);
        }
    }
};

int main()
{
    DirectoryLister::Main(Environment::GetCommandLineArgs());
}

穩固程式設計

在這個範例中,DirectoryInfo 是目前的目錄,以 (".") 表示,而且程式碼會列出此目前目錄中副檔名為 .exe 的所有檔案,以及檔案大小、建立時間和名稱。 假設在 C:\MyDir 的 \Bin 子目錄中有 .exe 檔案,則此程式碼的輸出面貌可能如下所示:

953          7/20/2000 10:42 AM   C:\MyDir\Bin\paramatt.exe
664          7/27/2000 3:11 PM    C:\MyDir\Bin\tst.exe
403          8/8/2000 10:25 AM    C:\MyDir\Bin\dirlist.exe

如果您想要另一個目錄中 (例如 C:\ 根目錄) 的檔案清單,請將引數 "C:\" 傳入編譯此程式碼所產生的可執行檔中,例如:"testApplication.exe C:\"。

注意事項注意事項

Visual Basic 使用者可以選擇對檔案 I/O 使用 FileSystem 類別所提供的方法和屬性。

請參閱

工作

HOW TO:讀取和寫入新建立的資料檔案

HOW TO:開啟並附加至記錄檔

HOW TO:從檔案讀取文字

HOW TO:將文字寫入檔案

HOW TO:從字串中讀取字元

HOW TO:將字元寫入至字串

參考

DirectoryInfo

CreationTime

FullName

FileInfo.Length

DirectoryInfo.GetFiles

概念

基本檔案 I/O