File.GetAttributes Method
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| path is empty, contains only white spaces, or contains invalid characters. | |
| 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. | |
| 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. |
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. 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. | |
| Write to a text file. | |
| Read from a text file. | |
| Append text to a file. | |
| Rename or move a file. | |
| Read from a binary file. | |
| Write to a binary file. | |
| Set file attributes. |
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"; // 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, static_cast<FileAttributes>(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";
// Delete the file if it exists.
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
- FileIOPermission for reading files. Associated enumeration: FileIOPermissionAccess.Read
Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.