首页 > 后端开发 > C#.Net教程 > C# 文件信息

C# 文件信息

WBOY
发布: 2024-09-03 15:23:39
原创
379 人浏览过

为了在.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
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板