File.SetAttributes Metoda
Definice
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řetížení
| SetAttributes(SafeFileHandle, FileAttributes) |
Nastaví zadaný FileAttributes soubor nebo adresář přidružený k |
| SetAttributes(String, FileAttributes) |
Nastaví zadaný FileAttributes soubor v zadané cestě. |
SetAttributes(SafeFileHandle, FileAttributes)
- Zdroj:
- File.cs
- Zdroj:
- File.cs
- Zdroj:
- File.cs
Nastaví zadaný FileAttributes soubor nebo adresář přidružený k fileHandle.
public:
static void SetAttributes(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle, System::IO::FileAttributes fileAttributes);
public static void SetAttributes (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle, System.IO.FileAttributes fileAttributes);
static member SetAttributes : Microsoft.Win32.SafeHandles.SafeFileHandle * System.IO.FileAttributes -> unit
Public Shared Sub SetAttributes (fileHandle As SafeFileHandle, fileAttributes As FileAttributes)
Parametry
- fileHandle
- SafeFileHandle
A SafeFileHandle k souboru nebo adresáři, pro který fileAttributes by mělo být nastaveno.
- fileAttributes
- FileAttributes
Bitová kombinace hodnot výčtu.
Výjimky
fileHandle je null.
Volající nemá požadované oprávnění.
Poznámky
Pomocí metody není možné změnit stav File komprese objektu SetAttributes(SafeFileHandle, FileAttributes) .
Platí pro
SetAttributes(String, FileAttributes)
- Zdroj:
- File.cs
- Zdroj:
- File.cs
- Zdroj:
- File.cs
Nastaví zadaný FileAttributes soubor v zadané cestě.
public:
static void SetAttributes(System::String ^ path, System::IO::FileAttributes fileAttributes);
public static void SetAttributes (string path, System.IO.FileAttributes fileAttributes);
static member SetAttributes : string * System.IO.FileAttributes -> unit
Public Shared Sub SetAttributes (path As String, fileAttributes As FileAttributes)
Parametry
- path
- String
Cesta k souboru.
- fileAttributes
- FileAttributes
Bitová kombinace hodnot výčtu.
Výjimky
Verze rozhraní .NET Framework a .NET Core starší než 2.1: path je prázdná, obsahuje pouze prázdné znaky, obsahuje neplatné znaky nebo je neplatný atribut souboru.
Zadaná cesta, název souboru nebo obojí překračují maximální délku definovanou systémem.
path je v neplatném formátu.
Zadaná cesta je neplatná (například se nachází na nenamapované jednotce).
Soubor nebyl nalezen.
path zadal(a) soubor, který je jen pro čtení.
-nebo-
Tato operace není na aktuální platformě podporovaná.
-nebo-
path zadal(a) adresář.
-nebo-
Volající nemá požadované oprávnění.
Příklady
Následující příklad ukazuje GetAttributes metody a SetAttributes použitím Archive atributů a Hidden na soubor.
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
String^ path = "c:\\temp\\MyTest.txt";
// Create the file if it does not exist.
if ( !File::Exists( path ) )
{
File::Create( path );
}
if ( (File::GetAttributes( path ) & FileAttributes::Hidden) == FileAttributes::Hidden )
{
// Show the file.
File::SetAttributes(path, File::GetAttributes( path ) & ~FileAttributes::Hidden);
Console::WriteLine( "The {0} file is no longer hidden.", path );
}
else
{
// Hide the file.
File::SetAttributes( path, static_cast<FileAttributes>(File::GetAttributes( path ) | FileAttributes::Hidden) );
Console::WriteLine( "The {0} file is now hidden.", path );
}
}
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// Create the file if it does not exist.
if (!File.Exists(path))
{
File.Create(path);
}
FileAttributes attributes = File.GetAttributes(path);
if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
{
// Show the file.
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
File.SetAttributes(path, attributes);
Console.WriteLine("The {0} file is no longer hidden.", path);
}
else
{
// Hide the file.
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
Console.WriteLine("The {0} file is now hidden.", path);
}
}
private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
}
open System.IO
open System.Text
let removeAttribute attributes attributesToRemove = attributes &&& ~~~attributesToRemove
let path = @"c:\temp\MyTest.txt"
// Create the file if it does not exist.
if File.Exists path |> not then
File.Create path |> ignore
let attributes = File.GetAttributes path
if attributes &&& FileAttributes.Hidden = FileAttributes.Hidden then
// Show the file.
let attributes =
removeAttribute attributes FileAttributes.Hidden
File.SetAttributes(path, attributes)
printfn $"The {path} file is no longer hidden."
else
// Hide the file.
File.SetAttributes(path, File.GetAttributes path ||| FileAttributes.Hidden)
printfn $"The {path} file is now hidden."
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
' Create the file if it does not exist.
If File.Exists(path) = False Then
File.Create(path)
End If
Dim attributes As FileAttributes
attributes = File.GetAttributes(path)
If (attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then
' Show the file.
attributes = RemoveAttribute(attributes, FileAttributes.Hidden)
File.SetAttributes(path, attributes)
Console.WriteLine("The {0} file is no longer hidden.", path)
Else
' Hide the file.
File.SetAttributes(path, File.GetAttributes(path) Or FileAttributes.Hidden)
Console.WriteLine("The {0} file is now hidden.", path)
End If
End Sub
Public Shared Function RemoveAttribute(ByVal attributes As FileAttributes, ByVal attributesToRemove As FileAttributes) As FileAttributes
Return attributes And (Not attributesToRemove)
End Function
End Class
Poznámky
Parametr path může zadat relativní nebo absolutní informace o cestě. Informace o relativní cestě jsou vykládány jako relativní k aktuálnímu pracovnímu adresáři. Pokud chcete získat aktuální pracovní adresář, přečtěte si téma GetCurrentDirectory.
Některé atributy souboru, například Hidden a ReadOnly, je možné kombinovat. Jiné atributy, například Normal, musí být použity samostatně.
Pomocí metody není možné změnit stav File komprese objektu SetAttributes .
Seznam běžných vstupně-výstupních úloh najdete v tématu Běžné vstupně-výstupní úlohy.
Viz také
- FileSystemWatcher
- Vstupně-výstupní operace souborů a Stream
- Čtení textu ze souboru
- Postupy: Zápis textu do souboru