📜  C#中文件和目录的基本操作

📅  最后修改于: 2021-05-29 13:47:08             🧑  作者: Mango

在本文中,我们将介绍如何创建,删除和重命名目录,以及如何删除和重命名文件。

创建目录

我们可以使用Directory类中存在的CreateDirectory()方法创建Directory。

// C# program to create a directory
using System;
using System.IO;
  
class Program {
  
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter a name for the new directory:");
  
        string DirName = Console.ReadLine();
  
        // Checking if string is empty or not
        if (DirName != String.Empty) 
        {
            // Creating the Directory
            Directory.CreateDirectory(DirName); 
  
            // Checking Directory is created
            // Successfully or not
            if (Directory.Exists(DirName)) 
            {
                Console.WriteLine("The directory was created!");
                Console.ReadKey();
            }
        }
    }
}

输出:

创建目录CSharp

您将在特定位置找到具有给定名称的目录。

重命名目录

using System;
using System.IO;
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter a  name of the directory to renamed:");
        string DirName = Console.ReadLine();
  
        // checking directory exsist or not
        if (Directory.Exists(DirName)) 
        {
            Console.WriteLine("Please enter a new name for this directory:");
            string newDirName = Console.ReadLine();
  
            if (newDirName != String.Empty) {
  
                // to rename directory
                Directory.Move(DirName, newDirName); 
  
                // checking directory has 
                // been renamed or not
                if (Directory.Exists(newDirName)) 
                {
                    Console.WriteLine("The directory was renamed to " + newDirName);
                    Console.ReadKey();
                }
            }
        }
    }
}

输出:

重命名目录CSharp

您将在特定位置找到更新的目录名称。没有称为Rename()的方法,因此我们使用Move()方法来重命名目录。移动和重命名在C#中是相同的操作。

删除目录

using System;
using System.IO;
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the directory name you want to delete:");
  
        string DirName = Console.ReadLine();
  
        // Checking if Directory Exist or not
        if (Directory.Exists(DirName)) 
        {
  
            // This will delete the 
            // Directory if it is empty
            Directory.Delete(DirName); 
  
            // checking if directory if 
            // deleted successfully or not
            if (Directory.Exists(DirName) == false) 
                Console.WriteLine("Directory deleted successfully...");
        }
        else
            Console.WriteLine("Directory {0} does not exist!", DirName);
        Console.ReadKey();
    }
}

输出:

删除目录-CSharp

您会发现在指定位置不再存在具有给定名称的目录。如果Directory不为空,则Delete()将引发异常,因为它会删除唯一的空目录。

Directory.Delete(DirName, true);

如果我们传递额外的参数,则Delete()方法是递归的。首先,在删除目录之前,将删除指定目录的所有文件和子目录。

重命名文件

// C# Program for Renaming a file
using System;
using System.IO;
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter a  name of the file to renamed:");
        string FileName = Console.ReadLine();
  
        // Checking File exsist or not
        if (File.Exists(FileName)) 
        {
            Console.WriteLine("Please enter a new name for this file:");
            string newFilename = Console.ReadLine();
  
            // Checking if string is null or not
            if (newFilename != String.Empty) 
            {
            
                // Renaming the file
                File.Move(FileName, newFilename); 
  
                // checking if the file has been
                // renamed successfully or not
                if (File.Exists(newFilename)) 
                {
                    Console.WriteLine("The file was renamed to " + newFilename);
                    Console.ReadKey();
                }
            }
        }
    }
}

输出:

重命名文件CSharp

您将在特定位置找到更改的文件名。

删除文件

using System;
using System.IO;
  
class GFG {
  
    // Main Method
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the file name you want to delete:");
        string FileName = Console.ReadLine();
  
        // Checking file exists or not
        if (File.Exists(FileName)) 
        {
  
            // Deleting the file
            File.Delete(FileName);
   
            // Checking if the file is deleted 
            // successfully or not
            if (File.Exists(FileName) == false) 
                Console.WriteLine("File deleted successfully...");
        }
        else
            Console.WriteLine("File {0} does not exist!", FileName);
        Console.ReadKey();
    }
}

输出:
删除文件CSharp

您会发现在特定位置不存在具有给定名称的此类文件。