FileStream.CanWrite-Eigenschaft
Ruft einen Wert ab, der angibt, ob der aktuelle Stream Schreibvorgänge unterstützt.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Overrides ReadOnly Property CanWrite As Boolean
'Usage
Dim instance As FileStream
Dim value As Boolean
value = instance.CanWrite
public override bool CanWrite { get; }
public:
virtual property bool CanWrite {
bool get () override;
}
/** @property */
public boolean get_CanWrite ()
public override function get CanWrite () : boolean
Eigenschaftenwert
true, wenn der Stream Schreibvorgänge unterstützt, false, wenn der Stream geschlossen ist oder mit schreibgeschütztem Zugriff geöffnet wurde.
Hinweise
Wenn eine von Stream abgeleitete Klasse keine Schreibvorgänge unterstützt, wird bei einem Aufruf von SetLength, Write, BeginWrite oder WriteByte eine NotSupportedException ausgelöst.
Wenn der Stream geschlossen ist, gibt diese Eigenschaft false zurück.
Beispiel
Im folgenden Beispiel wird mithilfe der CanWrite-Eigenschaft überprüft, ob ein Stream Schreibvorgänge unterstützt.
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
Das folgende Beispiel veranschaulicht die Verwendung der CanWrite-Eigenschaft. Die Ausgabe dieses Codes ist "MyFile.txt is writable". Wenn Sie die Ausgabemeldung "MyFile.txt can be both written to and read from." erhalten möchten, ändern Sie im FileStream-Konstruktor den FileAccess-Parameter in ReadWrite.
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();
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
FileStream-Klasse
FileStream-Member
System.IO-Namespace
Weitere Ressourcen
Datei- und Stream-E/A
Gewusst wie: Lesen aus einer Textdatei
Gewusst wie: Schreiben von Text in eine Datei