在.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);
}
}
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
C#实现猜数字游戏
本文实例为大家分享了c#实现猜数字游戏具体代码,供大家参考,具体内容如下给定一个0-100的随机数字猜其大小题目样式:电脑产生一个0到100之间的随机数字,并且...
[阅读全文]
-
-
C# 可空类型的具体使用
在项目中我们经常会遇到可为空类型,那么到底什么是可为空类型呢?下面我们将从4个方面为大家剖析。1、可空类型基础知识顾名思义,可空类型指的就是某个对象类型可以为空...
[阅读全文]
-
-
-
C#实现猜数字小游戏
本文实例为大家分享了c#实现猜数字小游戏的具体代码,供大家参考,具体内容如下效果如图:代码:using system;using system.collecti...
[阅读全文]
-
-
C#实现简单俄罗斯方块
最近在看《.net游戏编程入门经典 c#篇》 第一章介绍了如何制作俄罗斯方块,自己试了试按照书上的步骤,可算是完成了。于是写下这篇文章留作纪念。1.类的设计在充...
[阅读全文]
-
网友评论