File.OpenWrite(String) 方法

定义

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

public:
 static System::IO::FileStream ^ OpenWrite(System::String ^ path);
public static System.IO.FileStream OpenWrite (string path);
static member OpenWrite : string -> System.IO.FileStream
Public Shared Function OpenWrite (path As String) As FileStream

参数

path
String

要打开以进行写入的文件。

返回

指定路径上具有 FileStream 访问权限的非共享的 Write 对象。

例外

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

- 或 -

path 指定了一个只读文件或目录。

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

pathnull

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

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

path 的格式无效。

示例

以下示例打开一个用于读取和写入的文件。

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";

   // Open the stream and write to it.
   {
      FileStream^ fs = File::OpenWrite( path );
      try
      {
         array<Byte>^info = (gcnew UTF8Encoding( true ))->
            GetBytes( "This is to test the OpenWrite method." );

         // Add some information to the file.
         fs->Write( info, 0, info->Length );
      }
      finally
      {
         if ( fs )
            delete (IDisposable^)fs;
      }
   }

   // Open the stream and read it back.
   {
      FileStream^ fs = File::OpenRead( path );
      try
      {
         array<Byte>^b = gcnew array<Byte>(1024);
         UTF8Encoding^ temp = gcnew UTF8Encoding( true );
         while ( fs->Read( b, 0, b->Length ) > 0 )
         {
            Console::WriteLine( temp->GetString( b ) );
         }
      }
      finally
      {
         if ( fs )
            delete(IDisposable^)fs;
      }
   }
}
using System;
using System.IO;
using System.Text;

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

        // Open the stream and write to it.
        using (FileStream fs = File.OpenWrite(path))
        {
            Byte[] info =
                new UTF8Encoding(true).GetBytes("This is to test the OpenWrite method.");

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

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

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

let path = @"c:\temp\MyTest.txt"

// Open the stream and write to it.
do
    use fs = File.OpenWrite path

    let info =
        UTF8Encoding(true)
            .GetBytes "This is to test the OpenWrite method."

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

// Open the stream and read it back.
do
    use fs = File.OpenRead path
    let b = Array.zeroCreate 1024
    let temp = UTF8Encoding true

    while fs.Read(b, 0, b.Length) > 0 do
        printfn $"{temp.GetString b}"
Imports System.IO
Imports System.Text

Public Class Test
  Public Shared Sub Main()
    Dim path As String = "c:\temp\MyTest.txt"

    ' Open the stream and write to it.
    Using fs As FileStream = File.OpenWrite(path)
      Dim info As Byte() = _
       New UTF8Encoding(True).GetBytes("This is to test the OpenWrite method.")

      ' Add some information to the file.
      fs.Write(info, 0, info.Length)
    End Using

    'Open the stream and read it back.
    Using fs As FileStream = File.OpenRead(path)
      Dim b(1023) As Byte
      Dim temp As UTF8Encoding = New UTF8Encoding(True)

      Do While fs.Read(b, 0, b.Length) > 0
        Console.WriteLine(temp.GetString(b))
      Loop
    End Using

  End Sub
End Class

注解

此方法等效于 FileStream(String, FileMode, FileAccess, FileShare) 将文件模式设置为 OpenOrCreate的构造函数重载,将访问设置为 Write,共享模式设置为 None

如果文件路径中已存在文件,方法 OpenWrite 将打开一个文件;如果文件不存在,则创建一个新文件。 对于现有文件,它不会将新文本追加到现有文本。 相反,它会用新字符覆盖现有字符。 如果使用较短的字符串 ((如“Second run”) )覆盖较长的字符串 (“这是 OpenWrite 方法的测试”) ,则该文件将包含 (“OpenWrite 方法的第二次运行测试”) 的混合字符串。

参数 path 可以指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请使用 GetCurrentDirectory 方法。

返回的 FileStream 不支持读取。 若要打开文件进行读取和写入,请使用 Open

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

适用于

另请参阅