在.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类。

1.下面的这段代码演示了如何获得文件夹的信息,包括获得文件夹下的子文件夹,以及文件夹下的文件。这里使用了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);
}
}

2.下面演示了如何使用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()));
}

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

void DeleteFile1()
{
string fileToBeDeleted = @"c:\temp\New Text~ Document (3).txt";
if (File.Exists(fileToBeDeleted)) {
File.Delete(fileToBeDeleted);
}
}
void DeleteFile2() {
string fileToBeDeleted = @"c:\temp\New Text~ Document (3).txt";
FileInfo file = new FileInfo(fileToBeDeleted);
if (file.Exists) {
file.Delete();
}
}

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

void DeleteFolder1()
{
string folderToBeDeleted = @"c:\temp\test";
if (Directory.Exists(folderToBeDeleted)) {
// true is recursive delete:
Directory.Delete(folderToBeDeleted, true);
}
}
void DeleteFolder2() {
string folderToBeDeleted = @"c:\temp\test";
DirectoryInfo folder = new DirectoryInfo(folderToBeDeleted);
if (folder.Exists) {
folder.Delete(true);
}
}

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

void MoveFile1()
{
string fileToMove = @"c:\temp\New Text Document.txt";
string fileNewDestination = @"c:\temp\test.txt";
if (File.Exists(fileToMove) && !File.Exists(fileNewDestination)) {
File.Move(fileToMove, fileNewDestination);
}
}
void MoveFile2() {
string fileToMove = @"c:\temp\New Text Document.txt";
string fileNewDestination = @"c:\temp\test.txt";
FileInfo file = new FileInfo(fileToMove);
if (file.Exists) {
file.MoveTo(fileNewDestination);
}
}

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

void MoveFolder1()
{
string folderToMove = @"c:\temp\test";
string folderNewDestination = @"c:\temp\test2";
if (Directory.Exists(folderToMove)) {
Directory.Move(folderToMove, folderNewDestination);
}
}
void MoveFolder2() {
string folderToMove = @"c:\temp\test";
string folderNewDestination = @"c:\temp\test2";
DirectoryInfo folder = new DirectoryInfo(folderToMove);
if (folder.Exists) {
folder.MoveTo(folderNewDestination);
}
}

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

void CopyFile1()
{
string sourceFile = @"c:\temp\New Text Document.txt";
string destinationFile = @"c:\temp\test.txt";
if (File.Exists(sourceFile)) {
// true is overwrite
File.Copy(sourceFile, destinationFile, true);
}
}
void CopyFile2() {
string sourceFile = @"c:\temp\New Text Document.txt";
string destinationFile = @"c:\temp\test.txt";
FileInfo file = new FileInfo(sourceFile);
if (file.Exists) {
// true is overwrite
file.CopyTo(destinationFile, true);
}
}

  

 

C# 对文件与文件夹的操作包括删除、移动与复制的更多相关文章

  1. Linux常用操作:文件及文件夹

    一.创建 (1)mkdir 创建一个目录 (2)touch 创建一个空文件 注:-r 表示递归操作 二.删除 rm 删除文件 rm -r 删除目录 rm -r * 删除目录下的所有文件 注:-f 不用 ...

  2. java io流 对文件夹的操作

    java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...

  3. C# 文件和文件夹操作

    一.文件操作 1.File类的常用静态方法: void AppendAllText(string path, string contents),将文本contents附加到文件path中 bool E ...

  4. Python操作文件、文件夹、字符串

    Python 字符串操作 去空格及特殊符号 s.strip().lstrip().rstrip(',') 复制字符串 #strcpy(sStr1,sStr2) sStr1 = 'strcpy' sSt ...

  5. 【CITE】C#目录、文件、文件夹操作

    1.   在一个目录下创建一个文件夹 if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path); ...

  6. PYTHON对文件及文件夹的一些操作

    python中对文件.文件夹的操作需要涉及到os模块和shutil模块. 创建文件:1) os.mknod("test.txt") 创建空文件2) open("test. ...

  7. Java中创建操作文件和文件夹的工具类

    Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  8. Sharepoint中有关文件夹的操作

    1.GetItemsWithUniquePermissions根据返回数量和是否返回文件夹获取唯一权限的列表项集合 对于SharePoint对象模型中SPList的GetItemsWithUnique ...

  9. 如何在linux系统下对文件夹名有空格的文件夹进行操作

    http://www.2cto.com/os/201409/335119.html 在Windows操作系统中可以轻易地创建\移动\删除文件夹名带有空格的文件夹, 而在linux则需要进行一些特殊的处 ...

随机推荐

  1. Window10中创建目录连接点

    使用命令: mklink /J "junction point name" "target directory" 如,我有一个文件夹在D:\aa,想存在相同的目 ...

  2. Sqoop拒绝连接错误

    使用Sqoop远程连接MySQL导入数据到HBase数据库: sqoop import --driver com.mysql.jdbc.Driver --connect "jdbc:mysq ...

  3. selenium快速跳转视图到指定元素

    首先判断元素是否存在,如果存在的时候使用location_once_scrolled_into_view就可以滚动到某个元素处,也就是滚动直到这个元素出现在屏幕里.

  4. percona-Toolkit

    1:下载最新安装包 wget https://www.percona.com/downloads/percona-toolkit/2.1.1/percona-toolkit-2.1.1.tar.gz ...

  5. 【Bugly安卓开发干货】Android APP 高速 Pad 化实现

    Bugly 技术干货系列内容主要涉及移动开发方向.是由 Bugly 邀请腾讯内部各位技术大咖,通过日常工作经验的总结以及感悟撰写而成,内容均属原创.转载请标明出处. 怎样能在最快的时间内,实现一个最新 ...

  6. ES6学习笔记二:各种扩展

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7242967.html 一:字符串扩展 1:字符串遍历器 for (let char of str) { // ...

  7. CentOS 7 安装php5.6,Nginx,Memcached环境及配置

    安装php5.6版本以后不再需要安装Zend Guard,而是用yum命令安装php-opcache及php-pecl-apcu就可以有效的提高php执行速度. 1. 配置yum源 事先确认yum源的 ...

  8. CSS知识点集锦

      CSS知识点集锦 CreateTime--2016年9月29日09:43:10Author:Marydon UpdateTime--2017年3月21日08:03:13 2.CSS样式优先级问题 ...

  9. python模块之linecache

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python模块之linecache import linecache ''' >>> h ...

  10. Centos下和Win7下查看端口占用情况

    Centos #会列出所有正在使用的端口及关联的进程/应用 netstat -nap #portnumber要用具体的端口号代替,可以直接列出该端口听使用进程/应用 lsof -i :portnumb ...