C# 对文件与文件夹的操作包括删除、移动与复制_C#教程-查字典教程网
C# 对文件与文件夹的操作包括删除、移动与复制
C# 对文件与文件夹的操作包括删除、移动与复制
发布时间:2016-12-28 来源:查字典编辑
摘要:在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和Dir...

在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类。文件夹(Folder)是只在Windows操作系统中使用的名词。在操作系统的理论中,人们更习惯于使用目录(Directory)这个名词。或许微软为了有朝一日将.Net移植到其他的操作系统中(实际上也有很多人也在做着这个项目),所以还是以Directory来命名操作文件夹的类。

File类和Directory类都是静态类。使用它们的好处是不需要初始化对象。如果你对某一个文件或文件夹只进行一次操作,那你最好使用该静态类的静态方法,比如File.Move,File.Delete等等。如果你需要对一个文件或文件夹进行多次操作,那最好还是使用FileInfo和DirectoryInfo类。因为File类和Directory是静态类,所以你每次对一个文件或文件夹进行操作之前,它们都需要对该文件或文件夹进行一些检查,比如authentication。如果使用FileInfo类和DirectoryInfo类,只在初始化类的对象时进行相关的检查工作,也就是说只需要做一次,所以如果你需要对某个文件或文件夹进行多次操作,那最好使用FileInfo类和DirectoryInfo类。

下面的这段代码演示了如何获得文件夹的信息,包括获得文件夹下的子文件夹,以及文件夹下的文件。这里使用了DirectoryInfo 类来完成,当然你也可以使用Directory静态类。

复制代码 代码如下:

void DisplayFolder()

{

string folderFullName = @"c:temp";

DirectoryInfo theFolder = new DirectoryInfo(folderFullName);

if (!theFolder.Exists)

throw new DirectoryNotFoundException("Folder not found: " + folderFullName);

// list all subfolders in folder

Console.WriteLine("Subfolders:");

foreach (DirectoryInfo subFolder in theFolder.GetDirectories())

{

Console.WriteLine(subFolder.Name);

}

// list all files in folder

Console.WriteLine();

Console.WriteLine("Files:");

foreach (FileInfo file in theFolder.GetFiles())

{

Console.WriteLine(file.Name);

}

}

下面演示了如何使用FileInfo类来获得文件的相关信息,包括文件的创建日期,文件的大小等等。当然你同样也可以使用File静态类来完成。

复制代码 代码如下:

void DisplayFileInfo()

{

string folderFullName = @"c:temp";

string fileName = "New Text Document.txt";

string fileFullName = Path.Combine(folderFullName, fileName);

FileInfo theFile = new FileInfo(fileFullName);

if (!theFile.Exists)

throw new FileNotFoundException("File not found: " + fileFullName);

Console.WriteLine(string.Format("Creation time: {0}", theFile.CreationTime.ToString()));

Console.WriteLine(string.Format("Size: {0} bytes", theFile.Length.ToString()));

}

下面的代码分别使用了File类和FileInfo类来演示如何删除文件

复制代码 代码如下:

void DeleteFile1()

{

string fileToBeDeleted = @"c:tempNew Text~ Document (3).txt";

if (File.Exists(fileToBeDeleted))

{

File.Delete(fileToBeDeleted);

}

}

void DeleteFile2()

{

string fileToBeDeleted = @"c:tempNew Text~ Document (3).txt";

FileInfo file = new FileInfo(fileToBeDeleted);

if (file.Exists)

{

file.Delete();

}

}

下面的代码分别使用了Directory类和DirectoryInfo类来演示如何删除文件夹

复制代码 代码如下:

void DeleteFolder1()

{

string folderToBeDeleted = @"c:temptest";

if (Directory.Exists(folderToBeDeleted))

{

// true is recursive delete:

Directory.Delete(folderToBeDeleted, true);

}

}

void DeleteFolder2()

{

string folderToBeDeleted = @"c:temptest";

DirectoryInfo folder = new DirectoryInfo(folderToBeDeleted);

if (folder.Exists)

{

folder.Delete(true);

}

}

下面的代码分别使用了File类和FileInfo类来演示如何移动文件

复制代码 代码如下:

void MoveFile1()

{

string fileToMove = @"c:tempNew Text Document.txt";

string fileNewDestination = @"c:temptest.txt";

if (File.Exists(fileToMove) && !File.Exists(fileNewDestination))

{

File.Move(fileToMove, fileNewDestination);

}

}

void MoveFile2()

{

string fileToMove = @"c:tempNew Text Document.txt";

string fileNewDestination = @"c:temptest.txt";

FileInfo file = new FileInfo(fileToMove);

if (file.Exists)

{

file.MoveTo(fileNewDestination);

}

}

下面的代码分别使用了Directory类和DirectoryInfo类来演示如何移动文件夹

复制代码 代码如下:

void MoveFolder1()

{

string folderToMove = @"c:temptest";

string folderNewDestination = @"c:temptest2";

if (Directory.Exists(folderToMove))

{

Directory.Move(folderToMove, folderNewDestination);

}

}

void MoveFolder2()

{

string folderToMove = @"c:temptest";

string folderNewDestination = @"c:temptest2";

DirectoryInfo folder = new DirectoryInfo(folderToMove);

if (folder.Exists)

{

folder.MoveTo(folderNewDestination);

}

}

下面的代码分别使用了File类和FileInfo类来演示如何复制文件

复制代码 代码如下:

void CopyFile1()

{

string sourceFile = @"c:tempNew Text Document.txt";

string destinationFile = @"c:temptest.txt";

if (File.Exists(sourceFile))

{

// true is overwrite

File.Copy(sourceFile, destinationFile, true);

}

}

void CopyFile2()

{

string sourceFile = @"c:tempNew Text Document.txt";

string destinationFile = @"c:temptest.txt";

FileInfo file = new FileInfo(sourceFile);

if (file.Exists)

{

// true is overwrite

file.CopyTo(destinationFile, true);

}

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新C#教程学习
    热门C#教程学习
    编程开发子分类