当前位置: 移动技术网 > IT编程>开发语言>c# > C# 对文件与文件夹的操作包括删除、移动与复制

C# 对文件与文件夹的操作包括删除、移动与复制

2019年07月18日  | 移动技术网IT编程  | 我要评论
在.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:\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();
}
}

下面的代码分别使用了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);
}
}

下面的代码分别使用了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);
}
}

下面的代码分别使用了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);
}
}

下面的代码分别使用了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);
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网