C# 文件訊息

WBOY
發布: 2024-09-03 15:23:39
原創
261 人瀏覽過

為了在.NET框架中處理文件,使用的重要命名空間是system.IO命名空間,類似地,我們在C#中有一個名為FileInfo類的類,它不包含靜態方法,只有實例化對象才能使用該類,磁碟上的文件或網絡位置由fileinfo 對象表示,文件流對象可以在fileinfo 對象的幫助下創建,文件信息類提供實例方法來創建、刪除、複製、移動和打開文件我們可以更好地控製檔案的讀寫操作,使用fileinfo 類別手動編寫可以從檔案讀取的程式碼或可以寫入位元組的程式碼。

C# FileInfo 類別的語法如下:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class FileInfo : FileSystemInfo
登入後複製

C# FileInfo 類別的工作

要了解 C# 中 FileInfo 類別的工作原理,我們需要了解 FileInfo 類別的建構子、FileInfo 類別的屬性和 FileInfo 類別的方法。

FileInfo 類別的建構子解釋如下:

  • FileInfo(string): FileInfo 類別的新實例已初始化,它充當檔案路徑的包裝器。

FileInfo 類別有多個屬性。它們的解釋如下:

  • 屬性:我們可以使用 Attributes 屬性來取得或設定目前檔案或目前目錄的屬性。
  • CreationTime:我們可以使用 Creation Time 屬性來取得或設定目前檔案或目前目錄的建立時間。
  • 目錄:我們可以使用 Directory 屬性來取得父目錄的實例。
  • DirectoryName:我們可以使用 Directory Name 屬性來取得表示目錄完整路徑的字串。
  • 存在:我們可以使用 Exists 屬性來取得一個值來指示檔案是否存在。
  • FullName:我們可以使用 Full Name 屬性來取得目錄的完整路徑或檔案的完整路徑。
  • IsReadOnly:我們可以使用 Is Read Only 屬性來取得或設定一個值,該值可以確定目前檔案是否具有唯讀屬性。
  • LastAccessTime:我們可以透過使用Last access time屬性來取得或設定目前檔案或目前目錄的最後存取時間。
  • 長度:我們可以使用 length 屬性來取得目前檔案的大小(以位元組為單位)。
  • 名稱:我們可以使用 name 屬性來取得檔案的名稱。

FileInfo 類別有多種方法。它們的解釋如下:

  • AppendText(): A stream writer is created using this method AppendText(). The text is appended to the file which is represented by the instance of the FileInfo class by using this stream writer.
  • CopyTo(String): An existing file can be copied to a new file using this method CopyTo(String).
  • Create(): A file can be created using this method Create().
  • CreateText(): A stream writer is created using this method CreateText()  and this stream writer writes to a new text file.
  • Decrypt(): A file can be decrypted using this method decrypt() which was originally encrypted by using the encrypt method by the current account.
  • Delete(): A file can be deleted permanently using the Delete() method.
  • Encrypt(): A file can be encrypted using Encrypt() method and this file can be decrypted by using Decrypt() method provided the account used for encryption is the same account used for decryption also.
  • GetAccessControl(): A file security object is obtained using this method GetAccessControl()  and it encapsulates the entries of the Access Control List (ACL).
  • MoveTo(String): A specified file can be moved from one location to a newly specified location using MoveTo(String) method.
  • Open(File Mode): A file can be opened in a specified mode using the Open(File Mode) method.
  • OpenRead(): A file stream that can be read-only can be created using the OpenRead() method.
  • OpenText(): A stream reader can be created using this method OpenText() which can read from an existing file with UTF8 encoding.
  • OpenWrite(): A file stream that can be written only can be created using this method OpenWrite().
  • Refresh(): The state of the object can be refreshed using this method Refresh().
  • Replace(String, String): The contents of a specified file can be replaced by the contents of the other file which is described by the current object of the FileInfo class using this method Replace(String, String).
  • ToString(): The path is returned as a string using this method ToString().

As we have understood the constructors of FileInfo class, Properties of FileInfo class and methods of the FileInfo class, now consider the below program:

Code:

using System;
using System.IO;
namespace Program
{
class Check
{
static void Main(string[] args)
{
try
{
// the file location is specified where the file is to be created
string location = "C:\Users\shivakumarsh\Desktop\new.txt";
// instance of the fileinfo class is created
FileInfo file = new FileInfo(location);
// an empty file is created
file.Create();
Console.WriteLine("Creation of file is successfull");
}
catch(IOException e)
{
Console.WriteLine("Failed attempt to create file "+e);
}
}
}
}
登入後複製

Output:

C# 文件訊息

In the above program, a namespace called the program is declared. Then the main method consisting of the try-catch block is defined. The try block consists of the location string where the new file needs to be created. An instance of the file info class is created, and the location string is passed as a parameter to the instance of the file info class. Create () method is invoked on the object of the file info class to create a new file in the location specified by the location string. If the file creation is successful, the success message is printed otherwise an exception is raised which is included in the catch block.

Example of C# FileInfo

C# program to demonstrate usage of File Info class.

Code:

using System;
using System.IO;
namespace Program
{
class Check
{
static void Main(string[] args)
{
// the file location is specified where the file is to be located
string location = "C:\Users\shivakumarsh\Desktop\new.txt";
// instance of the fileinfo class is created
FileInfo file = new FileInfo(location);
// The specified file is deleted
file.Delete();
Console.WriteLine("Deletion of file is successfull");
}
}
}
登入後複製

Output:

C# 文件訊息

Conclusion

In this tutorial, we understand the concept of FileInfo class in C# through definition, constructors of FileInfo class, properties of FileInfo class, methods of FileInfo class, working of FileInfo class through examples.

以上是C# 文件訊息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!