FileSystem.FileOpen(Int32, String, OpenMode, OpenAccess, OpenShare, Int32) 方法


打开一个文件以进行输入或输出。Opens a file for input or output. 相比 FileOpenMy 功能可使文件 I/O 操作的效率更高、性能更好。The My feature gives you better productivity and performance in file I/O operations than FileOpen. 有关详细信息,请参阅 FileSystemFor more information, see FileSystem.

public static void FileOpen (int FileNumber, string FileName, Microsoft.VisualBasic.OpenMode Mode, Microsoft.VisualBasic.OpenAccess Access = Microsoft.VisualBasic.OpenAccess.Default, Microsoft.VisualBasic.OpenShare Share = Microsoft.VisualBasic.OpenShare.Default, int RecordLength = -1);
static member FileOpen : int * string * Microsoft.VisualBasic.OpenMode * Microsoft.VisualBasic.OpenAccess * Microsoft.VisualBasic.OpenShare * int -> unit
Public Sub FileOpen (FileNumber As Integer, FileName As String, Mode As OpenMode, Optional Access As OpenAccess = Microsoft.VisualBasic.OpenAccess.Default, Optional Share As OpenShare = Microsoft.VisualBasic.OpenShare.Default, Optional RecordLength As Integer = -1)



必需。Required. 任何有效文件数。Any valid file number. 可使用 FreeFile 函数获取下一个可用的文件号。Use the FreeFile function to obtain the next available file number.


必需。Required. 一个指定文件名的字符串表达式,其中可包含目录或文件夹以及驱动器。A string expression that specifies a file name - may include directory or folder, and drive.


必需。Required. 指定文件模式的枚举:AppendBinaryInputOutputRandomEnumeration specifying the file mode: Append, Binary, Input, Output, or Random. 有关更多信息,请参见OpenModeFor more information, see OpenMode .


可选。Optional. 一个枚举,它指定对打开的文件所允许进行的操作:ReadWriteReadWriteEnumeration specifying the operations permitted on the open file: Read, Write, or ReadWrite. 默认为 ReadWriteDefaults to ReadWrite. 有关更多信息,请参见OpenAccessFor more information, see OpenAccess .


可选。Optional. 一个枚举,它指定不允许其他进程对打开的文件所进行的操作:SharedLock ReadLock WriteLock Read WriteEnumeration specifying the operations not permitted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write. 默认为 Lock Read WriteDefaults to Lock Read Write. 有关更多信息,请参见OpenShareFor more information, see OpenShare .


可选。Optional. 小于或等于 32,767(字节)的数字。Number less than or equal to 32,767 (bytes). 对于为进行随机访问而打开的文件,此值是记录的长度。For files opened for random access, this value is the record length. 对于顺序文件,此值是缓冲的字符数。For sequential files, this value is the number of characters buffered.


记录长度是负数(并且不等于 -1)。Record length is negative (and not equal to -1).

FileName 已经打开,或 FileName 无效。FileName is already open, or FileName is invalid.


此示例演示了 FileOpen 函数对文件启用输入和输出的各种用法。This example illustrates various uses of the FileOpen function to enable input and output to a file.

下面的代码在 Input 模式下打开文件 TestFileThe following code opens the file TestFile in Input mode.

FileOpen(1, "TESTFILE", OpenMode.Input)
' Close before reopening in another mode.

此示例在 Binary 模式下打开文件,仅用于写入操作。This example opens the file in Binary mode for writing operations only.

FileOpen(1, "TESTFILE", OpenMode.Binary, OpenAccess.Write)
' Close before reopening in another mode.

下面的示例在 Random 模式下打开文件。The following example opens the file in Random mode. 此文件包含 Person结构的记录。The file contains records of the structure Person.

Structure Person
    <VBFixedString(30)> Dim Name As String
    Dim ID As Integer
End Structure
Public Sub ExampleMethod()
    ' Count 30 for the string, plus 4 for the integer.
    FileOpen(1, "TESTFILE", OpenMode.Random, , , 34)
    ' Close before reopening in another mode.
End Sub

此代码示例在 Output 模式下打开文件;任何进程都可以读取或写入文件。This code example opens the file in Output mode; any process can read or write to file.

FileOpen(1, "TESTFILE", OpenMode.Output, OpenAccess.Default, OpenShare.Shared)
' Close before reopening in another mode.

此代码示例在 Binary 模式下打开文件进行读取;其他进程无法读取文件。This code example opens the file in Binary mode for reading; other processes cannot read file.

FileOpen(1, "TESTFILE", OpenMode.Binary, OpenAccess.Read,


提供 FileOpen 函数是为了向后兼容,可能会影响性能。The FileOpen function is provided for backward compatibility and may affect performance. 对于非旧版应用程序,My.Computer.FileSystem 对象提供更好的性能。For non-legacy applications, the My.Computer.FileSystem object provides better performance. 有关详细信息,请参阅使用 Visual Basic 进行文件访问For more information, see File Access with Visual Basic.

必须先打开文件,然后才能对其执行任何 i/o 操作。You must open a file before any I/O operation can be performed on it. FileOpen 为文件的 i/o 分配一个缓冲区,并确定要用于缓冲区的访问模式。FileOpen allocates a buffer for I/O to the file and determines the mode of access to use with the buffer.


写入文件时,如果尝试写入文件的文件不存在,则应用程序可能需要创建一个文件。When writing to a file, an application may have to create a file, if the file to which it is trying to write does not exist. 为此,需要对要在其中创建文件的目录具有权限。To do so, it needs permission for the directory in which the file is to be created. 但是,如果 FileName 指定的文件存在,则应用程序只需要对文件本身具有 Write 权限。However, if the file specified by FileName does exist, the application needs Write permission only to the file itself. 若要帮助提高安全性,请在部署期间创建文件,并且仅授予对该文件的 Write 权限,而不是整个目录。Wherever possible, to help improve security, create the file during deployment and grant Write permission to that file only, instead of to the whole directory. 为了帮助提高安全性,请将数据写入用户目录,而不是写入到根目录或 Program Files 目录。To help improve security, write data to user directories instead of to the root directory or the Program Files directory.

可以通过使用 FreeFile() 函数查找要打开的通道。The channel to open can be found by using the FreeFile() function.


FileOpen 函数要求 ReadFileIOPermissionAccess 枚举进行访问,这可能会影响在部分信任情况下的执行。The FileOpen function requires Read access from the FileIOPermissionAccess enumeration, which may affect its execution in partial trust situations. 有关详细信息,请参阅 FileIOPermissionAccessFor more information, see FileIOPermissionAccess enumeration.