Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

File.SetAttributes Method

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

[Visual Basic]
Public Shared Sub SetAttributes( _
   ByVal path As String, _
   ByVal fileAttributes As FileAttributes _
)
[C#]
public static void SetAttributes(
 string path,
 FileAttributes fileAttributes
);
[C++]
public: static void SetAttributes(
 String* path,
 FileAttributes fileAttributes
);
[JScript]
public static function SetAttributes(
   path : String,
 fileAttributes : FileAttributes
);

Parameters

path
The path to the file.
fileAttributes
The desired FileAttributes, such as Hidden, ReadOnly, Normal, and Archive.

Exceptions

Exception Type Condition
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, such as being on an unmapped drive.

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.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Rename or move a file. File.Move

FileInfo.MoveTo

Get the attributes of a file. File.GetAttributes
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File

Example

[Visual Basic, C#, C++] The following example demonstrates the GetAttributes and SetAttributes methods by applying the Archive and Hidden attributes to a file.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' Delete the file if it exists.
        If File.Exists(path) = False Then
            File.Create(path)
        End If

        If (File.GetAttributes(path) And FileAttributes.Hidden) = FileAttributes.Hidden Then
            ' 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) Or FileAttributes.Hidden)
            Console.WriteLine("The {0} file is now hidden.", path)
        End If
    End Sub
End Class

[C#] 
using System;
using System.IO;
using System.Text;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        // Delete the file if it exists.
        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);
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";

    // Delete the file if it exists.
    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(S"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(S"The {0} file is now hidden.", path);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

File Class | File Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft