2024年2月20日发(作者:)

两者的共同点:

一:都用于典型的操作,如复制、移动、重命名、创建、打开、删除和追加到文件

二:默认情况下,将向所有用户授予对新文件的完全读/写访问权限。

两者的区别:

File类是静态类,由于所有的File方法都是静态的,所以如果只想执行一个操作,那么使用File方法的效率比使用相应的FileInfo 实例方法可能更高。所有的File方法都要求当前所操作的文件的路径。File 类的静态方法对所有方法都执行安全检查。如果打算多次重用某个对象,可考虑改用FileInfo的相应实例方法,因为并不总是需要安全检查。

file,directory可以控制多个文件所以进行每次安全检查,而FileInfo,DirectoryInfo只能控制一个文件信息只进行一次安全处理。

静态方法每次对文件进行操作过程是:静态方法存在于栈头,它是由类调用,然后寻找需要操作的文件。寻找需要操作文件的过程是个IO过程,耗时比较长。但它不必要到堆区去遍历实例化新对象。

普通方法是由当时的对象调用,需要创建对象,new一个,(静态方法不需要此过程)但如果操作次数多的话,普通方法就不需要再次去执行不必要而且耗时的IO操作,就能整体提速!

所以执行方法的次数也就能决定了使用哪个类的最佳选择。

参考《与从入门到精通》(电子工业出版社 Jones 著 高春蓉 谷宇 阎隽等译))

下面的示例演示了File类的一些主要成员。

using System;

using ;

class Test

...{

public static void Main()

...{

string path = @"c: ";

if (!(path))

...{

// Create a file to write to.

using (StreamWriter sw = Text(path))

...{

ine("Hello");

ine("And");

ine("Welcome");

}

}

// Open the file to read from.

using (StreamReader sr = xt(path))

...{

string s = "";

while ((s = ne()) != null)

...{

ine(s);

}

}

try

...{

string path2 = path + "temp";

// Ensure that the target does not exist.

(path2);

// Copy the file.

(path, path2);

ine("{0} was copied to {1}.", path, path2);

// Delete the newly created file.

(path2);

ine("{0} was successfully deleted.", path2);

}

catch (Exception e)

...{

ine("The process failed: {0}", ng());

}

}

}

方法 (String)

参数path:要创建的文件的路径及名称。

返回值:一个 FileStream,它提供对 path 中指定的文件的读/写访问。

下面的示例在指定路径中创建一个文件,将一些信息写入该文件,再从文件中读取。

using System;

using ;

using ;

class Test

...{

public static void Main()

...{

string path = @"c: ";

try

...{

// Delete the file if it exists.

if ((path))

...{

(path);

}

// Create the file.

using (FileStream fs = (path))

...{

Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");

// Add some information to the file.

(info, 0, );

}

// Open the stream and read it back.

using (StreamReader sr = xt(path))

...{

string s = "";

while ((s = ne()) != null)

...{

ine(s);

}

}

}

catch (Exception Ex)

...{

ine(ng());

}

}

}

xt 方法:打开现有 UTF-8 编码文本文件以进行读取。

参数path:要打开以进行读取的文件。

返回值:指定路径上的 StreamReader。

Text 方法:创建或打开一个文件用于写入 UTF-8 编码的文本。

参数path:要打开以进行写入的文件。

返回值:一个 StreamWriter,它使用 UTF-8 编码写入指定的文件。

下面的示例创建一个文件,用于写入和读取文本。

using System;

using ;

class Test

...{

public static void Main()

...{

string path = @"c: ";

if (!(path))

...{

// Create a file to write to.

using (StreamWriter sw = Text(path))

...{

ine("Hello");

ine("And");

ine("Welcome");

}

}

// Open the file to read from.

using (StreamReader sr = xt(path))

...{

string s = "";

while ((s = ne()) != null)

...{

ine(s);

}

}

}

}

下面的示例演示了 FileInfo 类的一些主要成员。

using System;

using ;

class Test

...{

public static void Main()

...{

string path = pFileName();

FileInfo fi1 = new FileInfo(path);

if (!)

...{

//Create a file to write to.

using (StreamWriter sw = Text())

...{

ine("Hello");

ine("And");

ine("Welcome");

}

}

//Open the file to read from.

using (StreamReader sr = xt())

...{

string s = "";

while ((s = ne()) != null)

...{

ine(s);

}

}

try

...{

string path2 = pFileName();

FileInfo fi2 = new FileInfo(path2);

//Ensure that the target does not exist.

();

//Copy the file.

(path2);

ine("{0} was copied to {1}.", path, path2);

//Delete the newly created file.

();

ine("{0} was successfully deleted.", path2);

}

catch (Exception e)

...{

ine("The process failed: {0}", ng());

}

}

}