C#文件夹的创建、删除、移动、复制等操作

C# 是一种编程语言,您可以使用它来进行文件夹操作。

以下是一些在 C# 中操作文件夹的示例代码:

创建文件夹

using System;
using System.IO;
namespace CreateDirectoryExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderName = @"C:\ExampleFolder";
            Directory.CreateDirectory(folderName);
            Console.WriteLine("Folder created successfully.");
        }
    }
}

删除文件夹

using System;
using System.IO;
namespace DeleteDirectoryExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderName = @"C:\ExampleFolder";
            Directory.Delete(folderName, true);
            Console.WriteLine("Folder deleted successfully.");
        }
    }
}

遍历文件夹内的文件

using System;
using System.IO;
namespace EnumerateFilesExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderName = @"C:\ExampleFolder";
            string[] files = Directory.GetFiles(folderName);
            Console.WriteLine("Files in the folder:");
            foreach (string file in files)
            {
                Console.WriteLine(file);
            }
        }
    }
}

这些代码只是 C# 中文件夹操作的示例,您可以根据自己的需求编写代码。希望对您有所帮助。


果糖网