Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

File.SetAttributes Method

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

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

public static void SetAttributes (
	string path,
	FileAttributes fileAttributes
)
public static void SetAttributes (
	String path, 
	FileAttributes fileAttributes
)
public static function SetAttributes (
	path : String, 
	fileAttributes : FileAttributes
)
Not applicable.

Parameters

path

The path to the file.

fileAttributes

The desired FileAttributes, such as Hidden, ReadOnly, Normal, and Archive.

Exception typeCondition

ArgumentException

path is empty, contains only white spaces, contains invalid characters, or the file attribute is invalid.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

NotSupportedException

path is in an invalid format.

DirectoryNotFoundException

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

FileNotFoundException

The file cannot be found.

UnauthorizedAccessException

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.

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.

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.

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

        if ((File.GetAttributes(path) & FileAttributes.Hidden) == FileAttributes.Hidden) 
        {
            // Show the file.
            File.SetAttributes(path, FileAttributes.Archive);
            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);
        }
    }
}

import System.*;
import System.IO.*;
import System.Text.*;

class Test
{
    public static void main(String[] args)
    {
        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).
            Equals(FileAttributes.Hidden)) {
            // Show the file.
            File.SetAttributes(path, FileAttributes.Archive);
            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);
        }
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.