Example 038 operation file

File class

Namespace: System.IO

Assembly: mscorlib.dll

Provides static methods for creating, copying, deleting, moving, and opening single files, and assists in creating  FileStream  objects.

annotation

Use the  File  class for typical operations such as copying, moving, renaming, creating, opening, deleting, and appending to a single file at a time. You can also use the  File  class to get and set file characteristics or DateTime  information related to file creation, access, and writing  . If you want to perform operations on multiple files, see  Directory.GetFiles  or  DirectoryInfo.GetFiles .

Many  File  methods return other i / o types when you create or open a file. You can use these other types to further manipulate the file. For details, see specific  File  members, such as  OpenText , CreateText, or  Create .

Since all  File  methods are static, it  may be more efficient to use the File  method (rather than the corresponding  FileInfo instance method) if you only want to perform one operation  . All  File  methods require the path of the file being processed.

 The static methods of the File class perform security checks on all methods. If you want to reuse an object multiple times, consider using   the corresponding instance method of FileInfo , because security checks are not always required.

By default, all users will be granted full read / write access to new files.

The following table describes the  enumeration used to customize the behavior of various  File methods.

Table 1
enumerate Explanation
FileAccess Specify read and write access to the file.
FileShare Specify the level of access allowed for files that are already in use.
FileMode Specify whether to keep or overwrite the contents of the existing file, and whether the request to create an existing file will raise an exception.

method

Table 2
AppendAllLines(String, IEnumerable<String>)

Append lines to a file, and then close the file. If the specified file does not exist, this method creates a file, writes the specified line to it, and then closes the file.

AppendAllLines(String, IEnumerable<String>, Encoding)

Append lines to a file using the specified encoding, and then close the file. If the specified file does not exist, this method creates a file, writes the specified line to it, and then closes the file.

AppendAllText(String, String)

Open a file, append the specified string to it, and then close the file. If the file does not exist, this method will create a file, write the specified string to the file, and then close the file.

AppendAllText(String, String, Encoding)

Append the specified character string to the file using the specified encoding, and create the file if the file does not already exist.

AppendText(String)

Create a  StreamWriter that appends UTF-8 encoded text to an existing file or a new file (if the specified file does not exist).

Copy(String, String)

Copy the existing file to the new file. It is not allowed to overwrite files with the same name.

Copy(String, String, Boolean)

Copy the existing file to the new file. Allow overwriting of files with the same name.

Create(String)

Create or overwrite the file in the specified path.

Create(String, Int32)

Create or overwrite the file in the specified path and specify the buffer size.

Create(String, Int32, FileOptions)

Create or overwrite the file in the specified path, specify the buffer size and an option that describes how to create or overwrite the file.

Create(String, Int32, FileOptions, FileSecurity)

Create or overwrite the file in the specified path, specify the buffer size, options describing how to create or overwrite the file, and values ​​used to determine the file's access control and audit security.

CreateText(String)

Create or open a file for writing UTF-8 encoded text. If the file already exists, its contents will be overwritten.

Delete(String)

Delete the specified file.

Exists(String)

Determine whether the specified file exists.

GetAttributes(String)

Get FileAttributes of files on this path  .

GetCreationTime(String)

Returns the creation date and time of the specified file or directory.

GetCreationTimeUtc(String)

Returns the creation date and time of the specified file or directory, the format is Coordinated Universal Time (UTC).

GetLastAccessTime(String)

Returns the date and time of the last access to the specified file or directory.

GetLastAccessTimeUtc(String)

Returns the date and time of the last access to the specified file or directory, the format is Coordinated Universal Time (UTC).

GetLastWriteTime(String)

Returns the date and time of the last write to the specified file or directory.

GetLastWriteTimeUtc(String)

Returns the date and time of the last write to the specified file or directory, the format is Coordinated Universal Time (UTC).

Move(String, String)

将指定文件移到新位置,提供要指定新文件名的选项。

Open(String, FileMode)

通过不共享的读/写访问权限打开指定路径上的 FileStream

Open(String, FileMode, FileAccess)

通过指定的模式和不共享的访问权限打开指定路径上的 FileStream

Open(String, FileMode, FileAccess, FileShare)

打开指定路径上的 FileStream,具有带读、写或读/写访问的指定模式和指定的共享选项。

OpenRead(String)

打开现有文件以进行读取。

OpenText(String)

打开现有 UTF-8 编码文本文件以进行读取。

OpenWrite(String)

打开一个现有文件或创建一个新文件以进行写入。

ReadAllBytes(String)

打开一个二进制文件,将文件的内容读入一个字节数组,然后关闭该文件。

ReadAllLines(String)

打开一个文本文件,读取文件的所有行,然后关闭该文件。

ReadAllLines(String, Encoding)

打开一个文件,使用指定的编码读取文件的所有行,然后关闭该文件。

ReadAllText(String)

打开一个文本文件,读取文件中的所有文本,然后关闭此文件。

ReadAllText(String, Encoding)

打开一个文件,使用指定的编码读取文件中的所有文本,然后关闭此文件。

ReadLines(String)

读取文件的行。

ReadLines(String, Encoding)

读取具有指定编码的文件的行。

Replace(String, String, String)

使用其他文件的内容替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。

Replace(String, String, String, Boolean)

用其他文件的内容替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份,还可以忽略合并错误。

SetAttributes(String, FileAttributes)

获取指定路径上的文件的指定 FileAttributes

SetCreationTime(String, DateTime)

设置创建该文件的日期和时间。

SetCreationTimeUtc(String, DateTime)

设置文件创建的日期和时间,其格式为协调通用时 (UTC)。

SetLastAccessTime(String, DateTime)

设置上次访问指定文件的日期和时间。

SetLastAccessTimeUtc(String, DateTime)

设置上次访问指定的文件的日期和时间,其格式为协调通用时 (UTC)。

SetLastWriteTime(String, DateTime)

设置上次写入指定文件的日期和时间。

SetLastWriteTimeUtc(String, DateTime)

设置上次写入指定的文件的日期和时间,其格式为协调通用时 (UTC)。

WriteAllBytes(String, Byte[])

创建一个新文件,在其中写入指定的字节数组,然后关闭该文件。 如果目标文件已存在,则覆盖该文件。

WriteAllLines(String, IEnumerable<String>)

创建一个新文件,向其中写入一个字符串集合,然后关闭该文件。

WriteAllLines(String, IEnumerable<String>, Encoding)

使用指定的编码创建一个新文件,向其中写入一个字符串集合,然后关闭该文件。

WriteAllLines(String, String[])

创建一个新文件,在其中写入指定的字节数组,然后关闭该文件。

WriteAllLines(String, String[], Encoding)

创建一个新文件,使用指定编码在其中写入指定的字符串数组,然后关闭该文件。

WriteAllText(String, String)

创建一个新文件,向其中写入指定的字符串,然后关闭文件。 如果目标文件已存在,则覆盖该文件。

WriteAllText(String, String, Encoding)

创建一个新文件,使用指定编码向其中写入指定的字符串,然后关闭文件。 如果目标文件已存在,则覆盖该文件。

一个示例:

Module Module1

    Sub Main()
        testDiskFile()
        Console.Read()
    End Sub
    Private Sub testDiskFile()
        Dim fileName As String = "test.txt"
        Dim fileDirectory As String = "j:\test"
        Dim FullFileName As String = fileDirectory & "\" & fileName

        Dim ReInfo As MsgBoxResult
        If Not IO.Directory.Exists(fileDirectory) Then
            ReInfo = MsgBox("文件夹不存在,是否创建?", MsgBoxStyle.Critical Or MsgBoxStyle.YesNo, "错误")
            If ReInfo = MsgBoxResult.Yes Then
                IO.Directory.CreateDirectory(fileDirectory)
            Else
                Exit Sub
            End If
        End If

        If IO.File.Exists(FullFileName) Then
            ReInfo = MsgBox("文件已存在,是否删除?", MsgBoxStyle.Exclamation Or MsgBoxStyle.YesNo, "提示")
            If ReInfo = MsgBoxResult.Yes Then
                IO.File.Decrypt(FullFileName)
            Else
                Exit Sub
            End If
        End If
        IO.File.Create(FullFileName)

    End Sub
End Module
 

发布了146 篇原创文章 · 获赞 0 · 访问量 2731

Guess you like

Origin blog.csdn.net/ngbshzhn/article/details/105611477