Bewerken

Share via


File.SetAttributes Method

Definition

Overloads

SetAttributes(SafeFileHandle, FileAttributes)

Sets the specified FileAttributes of the file or directory associated with fileHandle.

SetAttributes(String, FileAttributes)

Sets the specified FileAttributes of the file on the specified path.

SetAttributes(SafeFileHandle, FileAttributes)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Sets the specified FileAttributes of the file or directory associated with 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)

Parameters

fileHandle
SafeFileHandle

A SafeFileHandle to the file or directory for which fileAttributes should be set.

fileAttributes
FileAttributes

A bitwise combination of the enumeration values.

Exceptions

fileHandle is null.

The caller does not have the required permission.

Remarks

It is not possible to change the compression status of a File object using the SetAttributes(SafeFileHandle, FileAttributes) method.

Applies to

SetAttributes(String, FileAttributes)

Source:
File.cs
Source:
File.cs
Source:
File.cs

Sets the specified FileAttributes of the file on the specified path.

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

The path to the file.

fileAttributes
FileAttributes

A bitwise combination of the enumeration values.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path is empty, contains only white spaces, contains invalid characters, or the file attribute is invalid.

The specified path, file name, or both exceed the system-defined maximum length.

path is in an invalid format.

The specified path is invalid, (for example, it is on an unmapped drive).

The file cannot be found.

path specified a file that is read-only.

-or-

This operation is not supported on the current platform.

-or-

path specified a directory.

-or-

The caller does not have the required permission.

Examples

The following example demonstrates the GetAttributes and SetAttributes methods by applying the Archive and Hidden attributes to a file.

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

Remarks

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

Certain file attributes, such as Hidden and ReadOnly, can be combined. Other attributes, such as Normal, must be used alone.

It is not possible to change the compression status of a File object using the SetAttributes method.

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to