Stream.CanWrite Vlastnost
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Při přepsání v odvozené třídě získá hodnotu označující, zda aktuální datový proud podporuje zápis.
public:
abstract property bool CanWrite { bool get(); };
public abstract bool CanWrite { get; }
member this.CanWrite : bool
Public MustOverride ReadOnly Property CanWrite As Boolean
true
pokud datový proud podporuje psaní; v opačném případě . false
Následuje příklad použití CanWrite
vlastnosti .
using namespace System;
using namespace System::IO;
int main()
{
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
if ( fs->CanWrite )
{
Console::WriteLine( "MyFile.txt is writable." );
}
}
//This code outputs "MyFile.txt is writable."
//To get the output message "MyFile.txt can be both written to and read from.",
//change the FileAccess parameter to ReadWrite in the FileStream constructor.
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.");
}
}
}
//This code outputs "MyFile.txt is writable."
//To get the output message "MyFile.txt can be both written to and read from.",
//change the FileAccess parameter to ReadWrite in the FileStream constructor.
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.")
Else
If fs.CanWrite Then
Console.WriteLine("MyFile.txt is writable.")
End If
End If
End Sub
End Class
'This code outputs "MyFile.txt is writable."
'To get the output message "MyFile.txt can be both written to and read from.",
'change the FileAccess parameter to ReadWrite in the FileStream constructor.
Pokud třída odvozená z Stream nepodporuje psaní, volání Write, BeginWritenebo WriteByte vyvolá NotSupportedException. V takových případech se obvykle implementuje jako prázdná metoda, Flush aby byla zajištěna úplná kompatibilita s jinými Stream typy, protože je platné vyprázdnit datový proud jen pro čtení.
Produkt | Verze |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Zpětná vazba k produktu .NET
.NET je open source projekt. Vyberte odkaz pro poskytnutí zpětné vazby: