FileStream.CanWrite 屬性

定義

取得值,表示目前資料流是否支援寫入。

public:
 virtual property bool CanWrite { bool get(); };
public override bool CanWrite { get; }
member this.CanWrite : bool
Public Overrides ReadOnly Property CanWrite As Boolean

屬性值

如果資料流支援寫入,則為 true;如果資料流已關閉或以唯讀存取開啟,則為 false

範例

下列範例會 CanWrite 使用 屬性來檢查資料流程是否支援寫入。

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

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   // Ensure that the file is readonly.
   File::SetAttributes( path, static_cast<FileAttributes>(File::GetAttributes( path ) | FileAttributes::ReadOnly) );
   
   //Create the file.
   FileStream^ fs = gcnew FileStream( path,FileMode::OpenOrCreate,FileAccess::Read );
   try
   {
      if ( fs->CanWrite )
      {
         Console::WriteLine( "The stream for file {0} is writable.", path );
      }
      else
      {
         Console::WriteLine( "The stream for file {0} is not writable.", path );
      }
   }
   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";

        // Ensure that the file is readonly.
        File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);

        //Create the file.
        using (FileStream fs = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read))
        {
            if (fs.CanWrite)
            {
                Console.WriteLine("The stream for file {0} is writable.", path);
            }
            else
            {
                Console.WriteLine("The stream for file {0} is not writable.", path);
            }
        }
    }
}
open System.IO

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

// Ensure that the file is readonly.
File.SetAttributes(path, File.GetAttributes path ||| FileAttributes.ReadOnly)

//Create the file.
do
    use fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)

    if fs.CanWrite then
        printfn $"The stream for file {path} is writable."

    else
        printfn $"The stream for file {path} is not writable."
Imports System.IO

Public Class Test

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

        'Ensure that the file is readonly.
        File.SetAttributes(path, File.GetAttributes(path) Or FileAttributes.ReadOnly)

        'Create the file.
        Dim fs As FileStream = New FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)

        If fs.CanWrite Then
            Console.WriteLine("The stream connected to {0} is writable.", path)
        Else
            Console.WriteLine("The stream connected to {0} is not writable.", path)
        End If
        fs.Close()
    End Sub
End Class

以下是使用 屬性的 CanWrite 範例。 此程式碼的輸出為「MyFile.txt可寫入」。若要取得輸出訊息「MyFile.txt可以同時寫入和讀取。」,請將 FileAccess 建構函式中的 FileStream 參數變更為 ReadWrite

using namespace System;
using namespace System::IO;
int main( void )
{
   FileStream^ fs = gcnew FileStream( "MyFile.txt",FileMode::OpenOrCreate,FileAccess::Write );
   if ( fs->CanRead && fs->CanWrite )
   {
      Console::WriteLine( "MyFile.txt can be both written to and read from." );
   }
   else
   {
      Console::WriteLine( "MyFile.txt is writable." );
   }

   return 0;
}
using System;
using System.IO;

class TestRW
{
    public static void Main(String[] args)
    {
        FileStream fs = new FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Write);
        if (fs.CanRead && fs.CanWrite)
        {
            Console.WriteLine("MyFile.txt can be both written to and read from.");
        }
        else if (fs.CanWrite)
        {
            Console.WriteLine("MyFile.txt is writable.");
        }
    }
}
open System.IO

let fs = new FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Write)

if fs.CanRead && fs.CanWrite then
    printfn "MyFile.txt can be both written to and read from."
elif fs.CanWrite then
    printfn "MyFile.txt is writable."
Imports System.IO

Class TestRW
    Public Shared Sub Main()
        Dim fs As New FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Write)

        If fs.CanRead And fs.CanWrite Then
            Console.WriteLine("MyFile.txt can be both written to and read from.")
        ElseIf fs.CanWrite Then
            Console.WriteLine("MyFile.txt is writable.")
        End If
    End Sub
End Class

備註

如果衍生自 Stream 的類別不支援寫入,呼叫 SetLengthWriteBeginWriteWriteByteNotSupportedException 擲回 。

如果資料流程已關閉,這個屬性會傳 false 回 。

適用於

另請參閱