目录操作的知识点和思考练习
知识点:
Directory类
基本为静态方法,部分如下:
1、CreateDirectory 已重载。 创建指定路径中的所有目录。
Directory.CreateDirectory 方法 (String)
public static DirectoryInfo CreateDirectory (
string path
)
path
要创建的目录路径。
2、Delete 已重载。 删除指定的目录。
Directory.Delete 方法 (String) 从指定路径删除空目录。
public static void Delete (
string path
)
参数
path
要移除的空目录的名称。此目录必须为可写或为空。
Directory.Delete 方法 (String, Boolean) 删除指定的目录并(如果指示)删除该目录中的任何子目录。
public static void Delete (
string path,
bool recursive
)
参数
path
要移除的目录的名称。
recursive
若要移除 path 中的目录、子目录和文件,则为 true;否则为 false。
3、Exists 确定给定路径是否引用磁盘上的现有目录。
public static bool Exists (
string path
)
参数
path
要测试的路径。
返回值
如果 path 引用现有目录,则为 true;否则为 false。
4、Move 将文件或目录及其内容移到新位置。
public static void Move (
string sourceDirName,
string destDirName
)
参数
sourceDirName
要移动的文件或目录的路径。
destDirName
指向 sourceDirName 的新位置的路径。
5、参考的方法
GetCreationTime 获取目录的创建日期和时间。
GetCurrentDirectory 获取应用程序的当前工作目录。
GetDirectories 已重载。 获取指定目录中子目录的名称。
GetFiles 已重载。 返回指定目录中的文件的名称。
GetFileSystemEntries 已重载。 返回指定目录中所有文件和子目录的名称。
GetLastAccessTime 返回上次访问指定文件或目录的日期和时间。
GetLastWriteTime 返回上次写入指定文件或目录的日期和时间。
GetLogicalDrives 检索此计算机上格式为“<驱动器号>:\”的逻辑驱动器的名称。
GetParent 检索指定路径的父目录,包括绝对路径和相对路径。
思考练习:
using System; using System.Collections.Generic; using System.Text; using System.IO; namespace FileReadWrite { public class Program { static void Main() { Console.WriteLine(" 请输入要创建的目录所在的驱动器名"); string driverName = Console.ReadLine(); Console.WriteLine("请输入要创建的目录名"); string directoryName = Console.ReadLine(); string path = driverName + ":\\" + directoryName; Directory.CreateDirectory(path); Console.ReadLine(); } } }
浙公网安备 33010602011771号