FileStream.CanWrite Właściwość
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Pobiera wartość wskazującą, czy bieżący strumień obsługuje zapisywanie.
public:
virtual property bool CanWrite { bool get(); };
public override bool CanWrite { get; }
member this.CanWrite : bool
Public Overrides ReadOnly Property CanWrite As Boolean
Wartość właściwości
true
jeśli strumień obsługuje zapisywanie; false
jeśli strumień jest zamknięty lub został otwarty z dostępem tylko do odczytu.
Przykłady
W poniższym przykładzie użyto właściwości w CanWrite
celu sprawdzenia, czy strumień obsługuje zapisywanie.
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);
}
}
}
}
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
Poniżej przedstawiono przykład użycia CanWrite
właściwości . Dane wyjściowe tego kodu to "MyFile.txt można zapisywać". Aby uzyskać komunikat wyjściowy "MyFile.txt można zapisywać do i odczytywać z.", zmień FileAccess
parametr na ReadWrite
w konstruktorze FileStream
.
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.");
}
}
}
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
Uwagi
Jeśli klasa pochodząca z Stream klasy nie obsługuje pisania, wywołanie metody SetLength, Write, BeginWritelub WriteByte zgłasza wartość NotSupportedException.
Jeśli strumień jest zamknięty, ta właściwość zwraca false
wartość .