FileStream.CanWrite プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > FileStream.CanWrite プロパティの意味・解説 

FileStream.CanWrite プロパティ

現在のストリームが書き込みをサポートしているかどうかを示す値を取得します。

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説

Stream から派生したクラスが書き込みをサポートしていない場合に、SetLength、Write、BeginWrite、または WriteByte を呼び出すと、NotSupportedException がスローされます。

ストリームが閉じている場合、このプロパティは false を返します。

使用例使用例

CanWrite プロパティを使用して、ストリームが書き込みをサポートしているかどうかを確認する例を次に示します。

Imports System
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
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);
            }
        }
    }
}
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;
   }
}
import System.*;
import System.IO.*;
import System.Text.*;

class Test
{
    public static void main(String[]
 args)
    {
        String path = "c:\\temp\\MyTest.txt";

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

        //Create the file.
        FileStream fs = new FileStream(path, FileMode.OpenOrCreate,
 
            FileAccess.Read);
        try {
            if (fs.get_CanWrite()) {
                Console.WriteLine("The stream for file {0}
 is writable.",
                    path);
            }
            else {
                Console.WriteLine("The stream for file {0}
 is not writable.",
                    path);
            }
        }
        finally {
            fs.Dispose();
        }
    } //main
} //Test

CanWrite プロパティの使用例を次に示します。このコードの出力は "MyFile.txt is writable." となります。FileStream コンストラクタの FileAccess パラメータを ReadWrite に変更すると、"MyFile.txt can be both written to and read from." というメッセージが出力されます。

Imports System
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
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.");
        }
    }
}
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;
}

import System;
import System.IO;

class TestRW 
{
   public static function
 Main() : void 
   {
       var fs : FileStream = 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.");
    }
}
TestRW.Main();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

FileStream.CanWrite プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



FileStream.CanWrite プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS