File.GetAttributes 方法
获取在此路径上的文件的 FileAttributes。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Function GetAttributes ( _
path As String _
) As FileAttributes
用法
Dim path As String
Dim returnValue As FileAttributes
returnValue = File.GetAttributes(path)
public static FileAttributes GetAttributes (
string path
)
public:
static FileAttributes GetAttributes (
String^ path
)
public static FileAttributes GetAttributes (
String path
)
public static function GetAttributes (
path : String
) : FileAttributes
参数
- path
该文件的路径。
返回值
路径上文件的 FileAttributes。
异常
异常类型 | 条件 |
---|---|
path 为空,仅包含空白,或包含无效字符。 |
|
指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。 |
|
path 的格式无效。 |
|
path 表示一个文件且它是无效的,例如,位于未映射的驱动器上或无法找到文件。 |
|
path 表示一个目录且它是无效的,例如,位于未映射的驱动器上或无法找到目录。 |
备注
允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory。
有关使用此方法的示例,请参见“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
重命名或移动文件。 |
|
读取二进制文件。 |
|
写入二进制文件。 |
|
设置文件属性。 |
示例
下面的示例通过将 Archive 和 Hidden 属性应用于文件,演示了 GetAttributes 和 SetAttributes 方法。
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
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);
}
}
}
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
.NET Framework 安全性
- FileIOPermission 用于读取文件。关联的枚举: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
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0