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);

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

.NET 9 and other versions
Product Versions
.NET 7, 8, 9

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(string path, System.IO.FileAttributes 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 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;
    }
}

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

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0