这里我们主要用到Directory这个类来进行目录管理的
利用它,我们可以完成对目录的及其子目录的进行创建、移动、浏览等操作,甚至还可以定义隐藏目录和只读目录。
一、目录管理
其中参数path表示目录所在路径。
Directory的主要属性有:
1.Attributes:0X01表示只读,0X02表示隐藏
2.Name:当前路径名
3.Parent:上一级父目录名
4.Root:所在根目录名
5.CreationTime:目录创建时间
6.LastAccessTime:上次访问目录的时间
7.LastWriteTime:上次修改目录的时间
我们常常用到的一些Directory中的一些成员方法:
CreateDirectory(string path) 创建子目录
CreateDiectories(stirng path) 创建多级子目录
CreateFile(string filename) 当前目录下创建一个新文件
Delete() 删除目录
CreateDirectory | 已重载。 创建指定路径中的所有目录。 |
Delete | 已重载。 删除指定的目录。 |
Exists | 确定给定路径是否引用磁盘上的现有目录。 |
GetAccessControl | 已重载。 返回某个目录的 Windows 访问控制列表 (ACL)。 |
GetCreationTime | 获取目录的创建日期和时间。 |
GetCreationTimeUtc | 获取目录创建的日期和时间,其格式为协调通用时间 (UTC)。 |
GetCurrentDirectory | 获取应用程序的当前工作目录。 |
GetDirectories | 已重载。 获取指定目录中子目录的名称。 |
GetDirectoryRoot | 返回指定路径的卷信息、根信息或两者同时返回。 |
GetFiles | 已重载。 返回指定目录中的文件的名称。 |
GetFileSystemEntries | 已重载。 返回指定目录中所有文件和子目录的名称。 |
GetLastAccessTime | 返回上次访问指定文件或目录的日期和时间。 |
GetLastAccessTimeUtc | 返回上次访问指定文件或目录的日期和时间,其格式为协调通用时间 (UTC)。 |
GetLastWriteTime | 返回上次写入指定文件或目录的日期和时间。 |
GetLastWriteTimeUtc | 返回上次写入指定文件或目录的日期和时间,其格式为协调通用时间 (UTC)。 |
GetLogicalDrives | 检索此计算机上格式为“<驱动器号>:\”的逻辑驱动器的名称。 |
GetParent | 检索指定路径的父目录,包括绝对路径和相对路径。 |
Move | 将文件或目录及其内容移到新位置。 |
SetAccessControl | 将 DirectorySecurity 对象描述的访问控制列表 (ACL) 项应用于指定的目录。 |
SetCreationTime | 为指定的文件或目录设置创建日期和时间。 |
SetCreationTimeUtc | 设置指定文件或目录的创建日期和时间,其格式为协调通用时间 (UTC)。 |
SetCurrentDirectory | 将应用程序的当前工作目录设置为指定的目录。 |
SetLastAccessTime | 设置上次访问指定文件或目录的日期和时间。 |
SetLastAccessTimeUtc | 设置上次访问指定文件或目录的日期和时间,其格式为协调通用时间 (UTC)。 |
SetLastWriteTime | 设置上次写入目录的日期和时间。 |
SetLastWriteTimeUtc | 设置上次写入某个目录的日期和时间,其格式为协调通用时间 (UTC)。 |
Examples:
using System; using System.IO; class Test { public static void Main() { // Specify the directories you want to manipulate. string path = @"c:\MyDir"; string target = @"c:\TestDir"; try { // Determine whether the directory exists. if (!Directory.Exists(path)) { // Create the directory it does not exist. Directory.CreateDirectory(path); } if (Directory.Exists(target)) { // Delete the target to ensure it is not there. Directory.Delete(target, true); } // Move the directory. Directory.Move(path, target); // Create a file in the directory. File.CreateText(target + @"\myfile.txt"); // Count the files in the target directory. Console.WriteLine("The number of files in {0} is {1}", target, Directory.GetFiles(target).Length); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally {} } }
// The following example calculates the size of a directory // and its subdirectories, if any, and displays the total size // in bytes. using System; using System.IO; public class ShowDirSize { public static long DirSize(DirectoryInfo d) { long Size = 0; // Add file sizes. FileInfo[] fis = d.GetFiles(); foreach (FileInfo fi in fis) { Size += fi.Length; } // Add subdirectory sizes. DirectoryInfo[] dis = d.GetDirectories(); foreach (DirectoryInfo di in dis) { Size += DirSize(di); } return(Size); } public static void Main(string[] args) { if (args.Length != 1) { Console.WriteLine("You must provide a directory argument at the command line."); } else { DirectoryInfo d = new DirectoryInfo(args[0]); Console.WriteLine("The size of {0} and its subdirectories is {1} bytes.", d, DirSize(d)); } } }
该类是一个静态类,只能通过.方法名()来调用
不能用其来建对象,也就是不能用其来进行实例化
二、文件管理
File类通常和FileStream类协作来完成对文件的创建、删除、拷贝、移动、打开等操作
同样File类也有自己的属性,如绝对路径名DeriectoryName、创建时间CreationTime、上次访问时间LastAccessTime、上次修改时间LastWriteTime、文件长度Length等
AppendAllText | 已重载。 将指定的字符串追加到文件中,如果文件还不存在则创建该文件。 |
AppendText | 创建一个 StreamWriter,它将 UTF-8 编码文本追加到现有文件。 |
Copy | 已重载。 将现有文件复制到新文件。 |
Create | 已重载。 在指定路径中创建文件。 |
CreateText | 创建或打开一个文件用于写入 UTF-8 编码的文本。 |
Decrypt | 解密由当前帐户使用 Encrypt 方法加密的文件。 |
Delete | 删除指定的文件。如果指定的文件不存在,则不引发异常。 |
Encrypt | 将某个文件加密,使得只有加密该文件的帐户才能将其解密。 |
Exists | 确定指定的文件是否存在。 |
GetAccessControl | 已重载。 获取一个 FileSecurity 对象,它封装指定文件的访问控制列表 (ACL) 条目。 |
GetAttributes | 获取在此路径上的文件的 FileAttributes。 |
GetCreationTime | 返回指定文件或目录的创建日期和时间。 |
GetCreationTimeUtc | 返回指定的文件或目录的创建日期及时间,其格式为协调通用时间 (UTC)。 |
GetLastAccessTime | 返回上次访问指定文件或目录的日期和时间。 |
GetLastAccessTimeUtc | 返回上次访问指定的文件或目录的日期及时间,其格式为协调通用时间 (UTC)。 |
GetLastWriteTime | 返回上次写入指定文件或目录的日期和时间。 |
GetLastWriteTimeUtc | 返回上次写入指定的文件或目录的日期和时间,其格式为协调通用时间 (UTC)。 |
Move | 将指定文件移到新位置,并提供指定新文件名的选项。 |
Open | 已重载。 打开指定路径上的 FileStream。 |
OpenRead | 打开现有文件以进行读取。 |
OpenText | 打开现有 UTF-8 编码文本文件以进行读取。 |
OpenWrite | 打开现有文件以进行写入。 |
ReadAllBytes | 打开一个文件,将文件的内容读入一个字符串,然后关闭该文件。 |
ReadAllLines | 已重载。 打开一个文本文件,将文件的所有行都读入一个字符串数组,然后关闭该文件。 |
ReadAllText | 已重载。 打开一个文本文件,将文件的所有行读入一个字符串,然后关闭该文件。 |
Replace | 已重载。 使用其他文件的内容替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。 |
SetAccessControl | 对指定的文件应用由 FileSecurity 对象描述的访问控制列表 (ACL) 项。 | |
SetAttributes | 设置指定路径上文件的指定的 FileAttributes。 | |
SetCreationTime | 设置创建该文件的日期和时间。 | |
SetCreationTimeUtc | 设置文件创建的日期和时间,其格式为协调通用时间 (UTC)。 | |
SetLastAccessTime | 设置上次访问指定文件的日期和时间。 | |
SetLastAccessTimeUtc | 设置上次访问指定的文件的日期和时间,其格式为协调通用时间 (UTC)。 | |
SetLastWriteTime | 设置上次写入指定文件的日期和时间。 | |
SetLastWriteTimeUtc | 设置上次写入指定的文件的日期和时间,其格式为协调通用时间 (UTC)。 | |
WriteAllBytes | 创建一个新文件,在其中写入指定的字节数组,然后关闭该文件。如果目标文件已存在,则覆盖该文件。 | |
WriteAllLines | 已重载。 创建一个新文件,在其中写入指定的字符串,然后关闭文件。如果目标文件已存在,则覆盖该文件。 | |
WriteAllText | 已重载。 创建一个新文件,在文件中写入内容,然后关闭文件。如果目标文件已存在,则覆盖该文件。 |
Examples
using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; if (!File.Exists(path)) { // Create a file to write to. using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine("Hello"); sw.WriteLine("And"); sw.WriteLine("Welcome"); } } // Open the file to read from. using (StreamReader sr = File.OpenText(path)) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } try { string path2 = path + "temp"; // Ensure that the target does not exist. File.Delete(path2); // Copy the file. File.Copy(path, path2); Console.WriteLine("{0} was copied to {1}.", path, path2); // Delete the newly created file. File.Delete(path2); Console.WriteLine("{0} was successfully deleted.", path2); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } }