ホームページ  >  記事  >  バックエンド開発  >  C# ファイル情報

C# ファイル情報

WBOY
WBOYオリジナル
2024-09-03 15:23:39268ブラウズ

.NET Framework でファイルを操作するために使用される重要な名前空間は system.IO 名前空間です。同様に、C# には FileInfo クラスと呼ばれるクラスがあります。これは静的メソッドで構成されず、インスタンス化されたオブジェクトのみがこのクラスを使用できます。ディスク上のファイルまたはネットワークの場所は fileinfo オブジェクトによって表され、filestream オブジェクトは 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: Exists プロパティを使用して、ファイルが存在するかどうかを示す値を取得できます。
  • FullName: Full Name プロパティを使用して、ディレクトリの完全なパスまたはファイルの完全なパスを取得できます。
  • IsReadOnly: Is Read Only プロパティを使用して、現在のファイルが読み取り専用プロパティを持つかどうかを判断できる値を取得または設定できます。
  • LastAccessTime: 最終アクセス時刻プロパティを使用して、現在のファイルまたは現在のディレクトリに最後にアクセスした時刻を取得または設定できます。
  • 長さ: 長さプロパティを使用して、現在のファイルのサイズをバイト単位で取得できます。
  • 名前: 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。