File.Open 方法

定义

打开指定路径上的 FileStream

重载

Open(String, FileMode, FileAccess, FileShare)

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

Open(String, FileMode)

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

Open(String, FileStreamOptions)

使用指定的路径、创建模式、读/写和共享权限、其他 FileStream 对同一文件的访问权限、缓冲区大小、其他文件选项和分配大小初始化 类的新实例 FileStream

Open(String, FileMode, FileAccess)

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

Open(String, FileMode, FileAccess, FileShare)

Source:
File.cs
Source:
File.cs
Source:
File.cs

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

C#
public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share);

参数

path
String

要打开的文件。

mode
FileMode

FileMode 值,用于指定在文件不存在时是否创建该文件,并确定是保留还是覆盖现有文件的内容。

access
FileAccess

一个 FileAccess 值,它指定可以对文件执行的操作。

share
FileShare

一个 FileShare 值,它指定其他线程所具有的对该文件的访问类型。

返回

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

例外

.NET Framework 和 2.1 之前的 .NET Core 版本:path是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

- 或 -

access 指定了 Read,并且 mode 指定了 CreateCreateNewTruncateAppend

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

打开文件时发生 I/O 错误。

path 指定了一个只读文件,并且 access 不是 Read

- 或 -

path 指定了一个目录。

- 或 -

调用方没有所要求的权限。

- 或 -

modeCreate,指定文件为隐藏文件。

modeaccessshare 指定了一个无效值。

未找到 path 中指定的文件。

path 的格式无效。

示例

以下示例打开一个具有只读访问权限且不允许文件共享的文件。

C#
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // Create the file if it does not exist.
        if (!File.Exists(path))
        {
            // Create the file.
            using (FileStream fs = File.Create(path))
            {
                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");

                // Add some information to the file.
                fs.Write(info, 0, info.Length);
            }
        }

        // Open the stream and read it back.
        using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }

            try
            {
                // Try to get another handle to the same file.
                using (FileStream fs2 = File.Open(path, FileMode.Open))
                {
                    // Do some task here.
                }
            }
            catch (Exception e)
            {
                Console.Write("Opening the file twice is disallowed.");
                Console.WriteLine(", as expected: {0}", e.ToString());
            }
        }
    }
}

注解

允许 path 参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Open(String, FileMode)

Source:
File.cs
Source:
File.cs
Source:
File.cs

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

C#
public static System.IO.FileStream Open (string path, System.IO.FileMode mode);

参数

path
String

要打开的文件。

mode
FileMode

FileMode 值,用于指定在文件不存在时是否创建该文件,并确定是保留还是覆盖现有文件的内容。

返回

以读/写访问与不共享权限打开的指定模式和路径上的 FileStream

例外

.NET Framework 和 2.1 之前的 .NET Core 版本:path是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

打开文件时发生 I/O 错误。

path 指定了一个只读文件。

- 或 -

当前平台不支持此操作。

- 或 -

path 指定了一个目录。

- 或 -

调用方没有所要求的权限。

- 或 -

modeCreate,指定文件为隐藏文件。

mode 指定了一个无效值。

未找到 path 中指定的文件。

path 的格式无效。

示例

下面的代码示例创建一个临时文件,并向其写入一些文本。 然后,该示例使用 T:System.IO.FileMode.Open 打开文件;也就是说,如果文件尚不存在,则不会创建该文件。

C#
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        // Create a temporary file, and put some data into it.
        string path = Path.GetTempFileName();
        using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None))
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            // Add some information to the file.
            fs.Write(info, 0, info.Length);
        }

        // Open the stream and read it back.
        using (FileStream fs = File.Open(path, FileMode.Open))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}

注解

允许 path 参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Open(String, FileStreamOptions)

Source:
File.cs
Source:
File.cs
Source:
File.cs

使用指定的路径、创建模式、读/写和共享权限、其他 FileStream 对同一文件的访问权限、缓冲区大小、其他文件选项和分配大小初始化 类的新实例 FileStream

C#
public static System.IO.FileStream Open (string path, System.IO.FileStreamOptions options);

参数

path
String

要打开的文件的路径。

options
FileStreamOptions

描述要使用的可选 FileStream 参数的 对象。

返回

包装 FileStream 打开的文件的实例。

注解

FileStream(String, FileStreamOptions) 有关异常的信息。

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

Open(String, FileMode, FileAccess)

Source:
File.cs
Source:
File.cs
Source:
File.cs

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

C#
public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess access);

参数

path
String

要打开的文件。

mode
FileMode

FileMode 值,用于指定在文件不存在时是否创建该文件,并确定是保留还是覆盖现有文件的内容。

access
FileAccess

一个 FileAccess 值,它指定可以对文件执行的操作。

返回

一个非共享的 FileStream,它提供对指定文件的访问,并且具有指定的模式和访问权限。

例外

.NET Framework 和 2.1 之前的 .NET Core 版本:path是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

- 或 -

access 指定了 Read,并且 mode 指定了 CreateCreateNewTruncateAppend

pathnull

指定的路径和/或文件名超过了系统定义的最大长度。

指定的路径无效(例如,它位于未映射的驱动器上)。

打开文件时发生 I/O 错误。

path 指定了一个只读文件,并且 access 不是 Read

- 或 -

path 指定了一个目录。

- 或 -

调用方没有所要求的权限。

- 或 -

modeCreate,指定文件为隐藏文件。

modeaccess 指定了一个无效值。

未找到 path 中指定的文件。

path 的格式无效。

示例

以下示例打开一个具有只读访问权限的文件。

C#
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        // This sample assumes that you have a folder named "c:\temp" on your computer.
        string filePath = @"c:\temp\MyTest.txt";

        // Delete the file if it exists.
        if (File.Exists(filePath))
        {
            File.Delete(filePath);
        }

        // Create the file.
        using (FileStream fs = File.Create(filePath))
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            // Add some information to the file.
            fs.Write(info, 0, info.Length);
        }

        // Open the stream and read it back.
        using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);

            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }

            try
            {
                // Try to write to the file.
                fs.Write(b,0,b.Length);
            }
            catch (Exception e)
            {
                Console.WriteLine("Writing was disallowed, as expected: {0}", e.ToString());
            }
        }
    }
}

注解

允许 path 参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0