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í
| Name | Description |
|---|---|
| SetAttributes(SafeFileHandle, FileAttributes) |
Nastaví zadaný FileAttributes soubor nebo adresář přidružený . |
| SetAttributes(String, FileAttributes) |
Nastaví zadaný FileAttributes soubor na zadané cestě. |
SetAttributes(SafeFileHandle, FileAttributes)
- Zdroj:
- File.cs
- Zdroj:
- File.cs
- Zdroj:
- File.cs
- Zdroj:
- File.cs
- Zdroj:
- File.cs
Nastaví zadaný FileAttributes soubor nebo adresář přidružený .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
Do SafeFileHandle souboru nebo adresáře, pro který fileAttributes se má nastavit.
- 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
- Zdroj:
- File.cs
- Zdroj:
- File.cs
Nastaví zadaný FileAttributes soubor na 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
.NET Framework a verze .NET Core starší než 2.1: path je prázdný, obsahuje pouze prázdné znaky, obsahuje neplatné znaky nebo atribut souboru je neplatný.
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 je na nenamapované jednotce).
Soubor nebyl nalezen.
path zadal soubor, který je jen pro čtení.
nebo
Tato operace není na aktuální platformě podporovaná.
nebo
path zadal adresář.
nebo
Volající nemá požadované oprávnění.
Příklady
Následující příklad ukazuje GetAttributes a SetAttributes metody použitím Archive a Hidden atributů na soubor.
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á povoleno zadat relativní nebo absolutní informace o cestě. Relativní informace o cestě se interpretují jako relativní vzhledem k aktuálnímu pracovnímu adresáři. Aktuální pracovní adresář získáte v tématu GetCurrentDirectory.
Některé atributy souboru, například Hidden a ReadOnly, lze 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
- Vstup/výstup souborů a streamů
- Čtení textu ze souboru
- Postupy: Zápis textu do souboru