C#中 File,Directory,FileInfo,DirectoryInfo区别与应用
C#中 File,Directory,FileInfo,DirectoryInfo区别与应用
两者的共同点:
一:都用于典型的操作,如复制、移动、重命名、创建、打开、删除和追加到文件
二:默认情况下,将向所有用户授予对新文件的完全读/写访问权限。
两者的区别:
File类是静态类,由于所有的File方法都是静态的,所以如果只想执行一个操作,那么使用File方法的效率比使用相应的FileInfo 实例方法可能更高。所有的File方法都要求当前所操作的文件的路径。File 类的静态方法对所有方法都执行安全检查。如果打算多次重用某个对象,可考虑改用FileInfo的相应实例方法,因为并不总是需要安全检查。
file,directory可以控制多个文件所以进行每次安全检查,而FileInfo,DirectoryInfo只能控制一个文件信息只进行一次安全处理。
静态方法每次对文件进行操作过程是:静态方法存在于栈头,它是由类调用,然后寻找需要操作的文件。寻找需要操作文件的过程是个IO过程,耗时比较长。但它不必要到堆区去遍历实例化新对象。
普通方法是由当时的对象调用,需要创建对象,new一个,(静态方法不需要此过程)但如果操作次数多的话,普通方法就不需要再次去执行不必要而且耗时的IO操作,就能整体提速!
所以执行方法的次数也就能决定了使用哪个类的最佳选择。
参考《ASP.NET与VB.NET从入门到精通》(电子工业出版社 A.Rusell Jones 著 高春蓉 谷宇 阎隽等译))
下面的示例演示了File类的一些主要成员。
using System;
using System.IO;
class Test
...{
public static void Main()
...{
string path = @"c: empMyTest.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());
}
}
}
File.Create 方法 (String)
参数path:要创建的文件的路径及名称。
返回值:一个 FileStream,它提供对 path 中指定的文件的读/写访问。
下面的示例在指定路径中创建一个文件,将一些信息写入该文件,再从文件中读取。
using System;
using System.IO;
using System.Text;
class Test
...{
public static void Main()
...{
string path = @"c: empMyTest.txt";
try
...{
// Delete the file if it exists.
if (File.Exists(path))
...{
File.Delete(path);
}
// Create the file.
using (FileStream fs = File.Create(path))
...{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
...{
string s = "";
while ((s = sr.ReadLine()) != null)
...{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
...{
Console.WriteLine(Ex.ToString());
}
}
}
File.OpenText 方法:打开现有 UTF-8 编码文本文件以进行读取。
参数path:要打开以进行读取的文件。
返回值:指定路径上的 StreamReader。
File.CreateText 方法:创建或打开一个文件用于写入 UTF-8 编码的文本。
参数path:要打开以进行写入的文件。
返回值:一个 StreamWriter,它使用 UTF-8 编码写入指定的文件。
下面的示例创建一个文件,用于写入和读取文本。
using System;
using System.IO;
class Test
...{
public static void Main()
...{
string path = @"c: empMyTest.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);
}
}
}
}
下面的示例演示了 FileInfo 类的一些主要成员。
using System;
using System.IO;
class Test
...{
public static void Main()
...{
string path = Path.GetTempFileName();
FileInfo fi1 = new FileInfo(path);
if (!fi1.Exists)
...{
//Create a file to write to.
using (StreamWriter sw = fi1.CreateText())
...{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
//Open the file to read from.
using (StreamReader sr = fi1.OpenText())
...{
string s = "";
while ((s = sr.ReadLine()) != null)
...{
Console.WriteLine(s);
}
}
try
...{
string path2 = Path.GetTempFileName();
FileInfo fi2 = new FileInfo(path2);
//Ensure that the target does not exist.
fi2.Delete();
//Copy the file.
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
//Delete the newly created file.
fi2.Delete();
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (Exception e)
...{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
FileInfo.Open 方法 (FileMode):在指定的模式中打开文件。
参数 mode:一个 FileMode 常数,它指定打开文件所采用的模式(例如 Open 或 Append)。
返回值:在指定模式中打开、具有读/写访问权限且不共享的文件。
FileInfo.OpenRead 方法:该方法返回一个 FileShare 模式设置为 Read 的只读 FileStream 对象。
返回值:新的只读 FileStream 对象。
FileInfo.AppendText 方法:创建一个 StreamWriter,它向 FileInfo 的此实例表示的文件追加文本。
FileInfo.Create 方法 :创建文件。
下面的示例创建对文件的引用,然后使用 FileInfo.Create() 在磁盘上创建此文件。
using System;
using System.IO;
public class DeleteTest
...{
public static void Main()
...{
// Create a reference to a file.
FileInfo fi = new FileInfo("temp.txt");
// Actually create the file.
FileStream fs = fi.Create();
// Modify the file as required, and then close the file.
fs.Close();
// Delete the file.
fi.Delete();
}
}
下面的示例创建一个文件,向其中添加一些文本,然后从此文件中读取。
using System;
using System.IO;
using System.Text;
class Test
...{
public static void Main()
...{
string path = @"c: empMyTest.txt";
FileInfo fi = new FileInfo(path);
// Delete the file if it exists.
if (fi.Exists)
...{
fi.Delete();
}
//Create the file.
using (FileStream fs = fi.Create())
...{
Byte[] info =
new UTF8Encoding(true).GetBytes("This is some text in the file.");
//Add some information to the file.
fs.Write(info, 0, info.Length);
}
//Open the stream and read it back.
using (StreamReader sr = fi.OpenText())
...{
string s = "";
while ((s = sr.ReadLine()) != null)
...{
Console.WriteLine(s);
}
}
}
}
FileInfo.OpenText 方法:创建使用 UTF8 编码、从现有文本文件中进行读取的 StreamReader。
返回值:使用 UTF8 编码的新 StreamReader。
FileInfo.CreateText 方法:创建写入新文本文件的 StreamWriter。
返回值:新的StreamWriter。
下面的示例说明 CreateText 方法和OpenText 方法。
using System;
using System.IO;
class Test
...{
public static void Main()
...{
string path = @"c: empMyTest.txt";
FileInfo fi = new FileInfo(path);
if (!fi.Exists)
...{
//Create a file to write to.
using (StreamWriter sw = fi.CreateText())
...{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
//Open the file to read from.
using (StreamReader sr = fi.OpenText())
...{
string s = "";
while ((s = sr.ReadLine()) != null)
...{
Console.WriteLine(s);
}
}
}
}
分享到:
相关推荐
C#中File,Directory,FileInfo,DirectoryInfo区别与应用.pdf
C# File(文件类)、FileInfo(文件信息类)、Directory(目录类)、(目录信息类)
C#纯手写的可以浏览计算机内所有...里面用到了递归调用和C#中的File,FileInfo,Directory,DirectoryInfo,HashSet泛型,List泛型等类。实际应用不大,仅作为源码学习用,想学文档操作的朋友可以下回去看看,2分不贵。
File FileInfo Directory DirectoryInfo BinaryReader BinaryWriter Stream FileStream
C#对文件的操作相当方便,主要涉及到四个类:File、FileInfo、Directory、DirectoryInfo,前两个提供了针对文件的操作,后两个提供了针对目录的操作,类图关系如下: 本文举例详述了File类的用法。File中提供了许多...
流Stream,FileStream、BinaryReader与TextReade等类 File类、FileInfo、Directory 和DirectoryInfo等类 XmlReader、XmlWriter和XmlDocument类处理XML数据 使用和管理应用程序配置文件信息
非常不错的文件及文件夹的操作 常用的几个文件及其文件夹操作类 File FileInfo Directory DirectoryInfo 详见压缩包
C# 批量修改文件名 -文件及文件夹操作- File类、Directory 类、FileInfo 类、DirectoryInfo 类
第四部分 c#中应用微软office的excel实现各种二维及三维图形 第9章 应用程序中的excel图表 600 9.1 excel和c#间的互操作 600 9.2 c#应用程序中的excel图表示例 602 9.2.1 excel图表对象模型 602 9.2.2 创建独立...
本文实例讲述了C#使用Directoryinfo类获得目录信息和属性的方法。分享给大家供大家参考。具体如下: using System; using System.IO; class MainClass { static void Main(string[] args) { FileInfo file = new ...
C#文件流的详细操作,文件与文件夹创建复制移动删除,File类和Directory类的使用,FileInfo类和DirectoryInfo类的使用
12.1.2 FileInfo类和DirectoryInfo类 283 12.1.3 FileStream类 284 12.1.4 StreamWriter类 287 12.1.5 StreamReader类 289 12.2 文件的基本操作 290 12.2.1 判断文件是否存在 291 12.2.2 创建文件 291...
6.3 Directory类和DirectoryInfo类 140 6.4 例子:查找文件 143 6.5 例子:拆分和合并文件 144 习题: 145 第七章 多线程程序设计 146 7.1 线程类(Thread)的属性和方法 146 7.2 线程的创建 147 7.3 建立线程类 148 ...
本文实例讲述了C#遍历指定目录下所有文件的方法。分享给大家供大家参考。具体分析如下: ...foreach (FileInfo file in di.GetFiles()) { Console.WriteLine(File: {0}, file.Name); } 希望本文所述对大家的C#程
《轻松学C#(图解版)》完整扫描版================================================================ 基本信息 作者:谷涛、扶晓、毕国锋 丛书名:轻松学开发 出版社:电子工业出版社 ISBN:978-7-121-20223-0 出版...
在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类。文件夹(Folder)是只在Windows操作系统中使用的名词。在操作系统的理论中,人们更习惯于...
《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用...
“程序”可以使用与System.Console,System.Environment,System.IO.Path,System.IO.Directory,System.IO.File,System.IO.DirectoryInfo,System.IO.FileInfo相同的API 您可以添加代表游戏中存储设备的真实系统...
用VS编写的FTP服务器软件,C#网络程序编程学习用。 代码: using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net; using System.Net.Sockets; using ...
3. DirectoryInfo类和Directory类实现对目录的操作 4. FileInfo和File类实现对文件的操作 5. Path类对路径的操作 6. C#流的介绍 7. StreamReader读取文件 8. StreamWriter类:写入文件 9. FileStream类:文件...