Rumah >pembangunan bahagian belakang >Tutorial C#.Net >C# FileInfo
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
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:
Terdapat beberapa sifat kelas FileInfo. Ia dijelaskan seperti berikut:
Terdapat beberapa kaedah kelas FileInfo. Ia dijelaskan seperti berikut:
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:
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.
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:
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!