溫馨提示×

DirectoryInfo怎樣創(chuàng)建文件夾

小樊
88
2024-07-06 19:36:14
欄目: 編程語言

您可以使用DirectoryInfo類的CreateSubdirectory方法來創(chuàng)建文件夾。以下是一個示例代碼:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = @"C:\example\newfolder";

        // 創(chuàng)建一個DirectoryInfo對象
        DirectoryInfo directory = new DirectoryInfo(path);

        // 檢查文件夾是否存在,如果不存在則創(chuàng)建
        if (!directory.Exists)
        {
            directory.Create();
            Console.WriteLine("文件夾已創(chuàng)建成功!");
        }
        else
        {
            Console.WriteLine("文件夾已存在!");
        }
    }
}

在上面的示例中,我們首先指定新文件夾的路徑,然后使用DirectoryInfo類創(chuàng)建一個DirectoryInfo對象。接著我們檢查文件夾是否存在,如果不存在則使用Create方法創(chuàng)建文件夾。最后輸出相應(yīng)的消息。

0