FileStream.CanWrite プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)


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

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();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からFileStream.CanWrite プロパティを検索する場合は、下記のリンクをクリックしてください。

- FileStream.CanWrite プロパティのページへのリンク