File.GetAttributes Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
GetAttributes(SafeFileHandle) |
Gets the specified FileAttributes of the file or directory associated with |
GetAttributes(String) |
Gets the FileAttributes of the file on the path. |
GetAttributes(SafeFileHandle)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
Gets the specified FileAttributes of the file or directory associated with fileHandle
.
public:
static System::IO::FileAttributes GetAttributes(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle);
public static System.IO.FileAttributes GetAttributes (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle);
static member GetAttributes : Microsoft.Win32.SafeHandles.SafeFileHandle -> System.IO.FileAttributes
Public Shared Function GetAttributes (fileHandle As SafeFileHandle) As FileAttributes
Parameters
- fileHandle
- SafeFileHandle
A SafeFileHandle to the file or directory for which the attributes are to be retrieved.
Returns
The FileAttributes of the file or directory.
Exceptions
fileHandle
is null
.
The caller does not have the required permission.
Applies to
GetAttributes(String)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
Gets the FileAttributes of the file on the path.
public:
static System::IO::FileAttributes GetAttributes(System::String ^ path);
public static System.IO.FileAttributes GetAttributes (string path);
static member GetAttributes : string -> System.IO.FileAttributes
Public Shared Function GetAttributes (path As String) As FileAttributes
Parameters
- path
- String
The path to the file.
Returns
The FileAttributes of the file on the path.
Exceptions
.NET Framework and .NET Core versions older than 2.1: path
is empty, contains only white spaces, or contains invalid characters.
The specified path, file name, or both exceed the system-defined maximum length.
path
is in an invalid format.
path
represents a file and is invalid, such as being on an unmapped drive, or the file cannot be found.
path
represents a directory and is invalid, such as being on an unmapped drive, or the directory cannot be found.
This file is being used by another process.
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.
For a list of common I/O tasks, see Common I/O Tasks.