Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the FileInfo class for file manipulation in C #

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the file operation FileInfo class in C#. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.

The FileInfo class provides the same functionality as the File class, except that FileInfo provides member methods

1. Read the file

/ / Summary: create a read-only System.IO.FileStream. Public System.IO.FileStream OpenRead () / / Summary: create a System.IO.StreamReader that uses UTF8 encoding to read from an existing text file. Public System.IO.StreamReader OpenText ()

2. Write documents

/ / Summary: create a write-only System.IO.FileStream. Public System.IO.FileStream OpenWrite ()

3. Additional content

/ / Summary: create a System.IO.StreamWriter that appends text to the file represented by this instance of System.IO.FileInfo. Public System.IO.StreamWriter AppendText ()

4. Open the file

/ / Summary: opens the file in the specified mode. Public System.IO.FileStream Open (System.IO.FileMode mode) / / Summary: opens a file in the specified mode with read, write, or read / write access. Public System.IO.FileStream Open (System.IO.FileMode mode, System.IO.FileAccess access) / / Summary: opens a file in the specified mode with read, write, or read / write access and specified sharing options. Public System.IO.FileStream Open (System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share)

5. Copy, move, replace

/ / Summary: copy an existing file to a new file, overwriting the existing file is not allowed. Public System.IO.FileInfo CopyTo (string destFileName) / / Summary: copies an existing file to a new file, allowing existing files to be overwritten. Public System.IO.FileInfo CopyTo (string destFileName, bool overwrite) / / Summary: moves the specified file to a new location and provides the option to specify a new file name. Public void MoveTo (string destFileName) / / Summary: replaces the contents of the specified file with the file described by the current System.IO.FileInfo object, which deletes the original file and creates a backup of the file being replaced. Public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName) / / Summary: replaces the contents of the specified file with the file described by the current System.IO.FileInfo object, which deletes the original file and creates a backup of the file being replaced. Also specifies whether to ignore merge errors. Public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)

6. Encryption, decryption and deletion

/ / Summary: encrypts a file so that only the account that encrypts the file can decrypt it. Public void Encrypt () / / Summary: decrypts files encrypted by the current account using the System.IO.FileInfo.Encrypt () method. Public void Decrypt () / / Summary: permanently delete files. Public override void Delete ()

7. Get file attributes

/ / Summary: get the instance of the parent directory. Public System.IO.DirectoryInfo Directory {get;} / / Summary: gets a string that represents the full path to the directory. Public string DirectoryName {get;} / / Summary: gets a value indicating whether the file exists. Public override bool Exists {get;} / / Summary: gets or sets a value that determines whether the current file is read-only. Public bool IsReadOnly {set; get;} / / Summary: gets the size of the current file (in bytes). Public long Length {get;} / / Summary: get the file name. Public override string Name {get;}

Getting the relevant properties of a file in FileInfo is no longer a method, it is all obtained through properties, and all properties are read-only except whether the read-only attribute is read-and-write.

These are all the contents of this article entitled "how to use the FileInfo class for file manipulation in C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report