File.SetAttributes(String, FileAttributes) Methode

Definitie

Hiermee stelt u de opgegeven FileAttributes van het bestand op het opgegeven pad in.

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)

Parameters

path
String

Het pad naar het bestand.

fileAttributes
FileAttributes

Een bitsgewijze combinatie van de opsommingswaarden.

Uitzonderingen

.NET Framework en .NET Core-versies ouder dan 2.1: path leeg is, bevat alleen spaties, bevat ongeldige tekens of het bestandskenmerk is ongeldig.

Het opgegeven pad, de bestandsnaam of beide overschrijden de door het systeem gedefinieerde maximumlengte.

path heeft een ongeldige indeling.

Het opgegeven pad is ongeldig (bijvoorbeeld op een niet-toegewezen station).

Kan het bestand niet vinden.

path een bestand opgegeven dat het kenmerk Alleen-lezen heeft.

– of –

Deze bewerking wordt niet ondersteund op het huidige platform.

– of –

path een map opgegeven.

– of –

De beller heeft niet de vereiste machtiging.

Voorbeelden

In het volgende voorbeeld ziet u de GetAttributes en SetAttributes methoden door de Archive en Hidden kenmerken toe te passen op een bestand.

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

Opmerkingen

De path parameter mag relatieve of absolute padgegevens opgeven. Relatieve padinformatie wordt geïnterpreteerd als relatief ten opzichte van de huidige werkmap. Als u de huidige werkmap wilt ophalen, raadpleegt GetCurrentDirectoryu .

Bepaalde bestandskenmerken, zoals Hidden en ReadOnly, kunnen worden gecombineerd. Andere kenmerken, zoals Normal, moeten alleen worden gebruikt.

Het is niet mogelijk om de compressiestatus van een File object te wijzigen met behulp van de SetAttributes methode.

Zie Algemene I/O-taken voor een lijst met algemene I/O-taken.

Van toepassing op

Zie ook