1. 程式人生 > >file與fileinfo的區別

file與fileinfo的區別

兩者的共同點:
一:都用於典型的操作,如複製、移動、重新命名、建立、開啟、刪除和追加到檔案
二:預設情況下,將向所有使用者授予對新檔案的完全讀/寫訪問許可權。

兩者的區別:
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());
}
}
}