Untuk bekerja dengan fail dalam rangka kerja .NET, ruang nama penting yang digunakan ialah ruang nama system.IO dan begitu juga, kami mempunyai kelas yang dipanggil kelas FileInfo dalam C# yang tidak terdiri daripada kaedah statik dan hanya objek instantiated boleh menggunakan kelas ini, a fail pada cakera atau lokasi rangkaian diwakili oleh objek fileinfo, objek filestream boleh dibuat dengan bantuan objek fileinfo dan kaedah contoh disediakan oleh kelas maklumat fail untuk mencipta, memadam, menyalin, memindahkan dan membuka fail dan kita boleh mempunyai lebih kawalan ke atas operasi membaca dan menulis pada fail untuk menulis kod secara manual yang boleh dibaca atau bait boleh ditulis daripada fail menggunakan kelas failinfo.
Sintaks kelas C# FileInfo adalah seperti berikut:
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class FileInfo : FileSystemInfo
Salin selepas log masuk
Kerja kelas C# FileInfo
Untuk memahami kerja kelas FileInfo dalam C#, kita perlu memahami pembina kelas FileInfo, sifat kelas FileInfo dan kaedah kelas FileInfo.
Pembina kelas FileInfo dijelaskan seperti di bawah:
-
FileInfo(rentetan): Satu kejadian baharu kelas FileInfo dimulakan dan ia bertindak sebagai pembungkus untuk laluan fail.
Terdapat beberapa sifat kelas FileInfo. Ia dijelaskan seperti berikut:
-
Atribut: Kami boleh mendapatkan atau menetapkan atribut untuk fail semasa atau direktori semasa menggunakan sifat Atribut.
-
Masa Penciptaan: Kita boleh mendapatkan atau menetapkan masa penciptaan untuk fail semasa atau direktori semasa menggunakan sifat Masa Penciptaan.
-
Direktori: Kami boleh mendapatkan contoh direktori induk menggunakan sifat Direktori.
-
Nama Direktori: Kita boleh mendapatkan rentetan yang mewakili laluan penuh direktori menggunakan sifat Nama Direktori.
-
Wujud: Kita boleh mendapatkan nilai yang menunjukkan sama ada fail wujud atau tiada menggunakan sifat Wujud.
-
Nama Penuh: Kita boleh mendapatkan laluan penuh direktori atau laluan penuh fail menggunakan sifat Nama Penuh.
-
IsReadOnly: Kami boleh mendapatkan atau menetapkan nilai yang boleh menentukan sama ada fail semasa mempunyai sifat baca sahaja menggunakan sifat Is Read Only.
-
LastAccessTime: Kami boleh mendapatkan atau menetapkan masa di mana fail semasa atau direktori semasa terakhir diakses dengan menggunakan sifat Masa akses terakhir.
-
Panjang: Kita boleh mendapatkan saiz fail semasa dalam bait menggunakan sifat panjang.
-
Nama: Kita boleh mendapatkan nama fail dengan menggunakan sifat nama.
Terdapat beberapa kaedah kelas FileInfo. Ia dijelaskan seperti berikut:
-
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);
}
}
}
}
Salin selepas log masuk
Output:
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");
}
}
}
Salin selepas log masuk
Output:
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.
Atas ialah kandungan terperinci C# FileInfo. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!