FileStream.CanWrite Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém um valor que indica se o fluxo atual suporta escrita.
public:
virtual property bool CanWrite { bool get(); };
public override bool CanWrite { get; }
member this.CanWrite : bool
Public Overrides ReadOnly Property CanWrite As Boolean
Valor de Propriedade
true se o fluxo suportar a escrita; false se o fluxo estiver fechado ou foi aberto com acesso apenas de leitura.
Exemplos
O exemplo seguinte usa a CanWrite propriedade para verificar se um fluxo suporta escrita.
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);
}
}
}
}
open System.IO
let path = @"c:\temp\MyTest.txt"
// Ensure that the file is readonly.
File.SetAttributes(path, File.GetAttributes path ||| FileAttributes.ReadOnly)
//Create the file.
do
use fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)
if fs.CanWrite then
printfn $"The stream for file {path} is writable."
else
printfn $"The stream for file {path} is not writable."
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
Segue-se um exemplo usando a CanWrite propriedade. A saída deste código é "MyFile.txt é gravável." Para obter a mensagem de saída "MyFile.txt pode ser tanto escrita como lida de.", altere o FileAccess parâmetro para ReadWrite no FileStream construtor.
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.");
}
}
}
open System.IO
let fs = new FileStream("MyFile.txt", FileMode.OpenOrCreate, FileAccess.Write)
if fs.CanRead && fs.CanWrite then
printfn "MyFile.txt can be both written to and read from."
elif fs.CanWrite then
printfn "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
Observações
Se uma classe derivada de Stream não suportar escrita, uma chamada para SetLength, Write, BeginWrite, ou WriteByte lança um NotSupportedException.
Se o fluxo estiver fechado, esta propriedade devolve false.