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 쓰고 읽을 수 있습니다."라는 출력 메시지를 얻으려면 생성자에서 매개 변수를 ReadWriteFileStream 로 변경 FileAccess 합니다.

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 파생된 클래스가 쓰기를 지원하지 않는 경우 , , WriteBeginWrite또는 WriteByte 를 호출하면 SetLengthNotSupportedExceptionthrow됩니다.

스트림이 닫혀 있으면 이 속성은 를 반환합니다 false.

적용 대상

추가 정보