File.SetAttributes(String, FileAttributes) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取指定路径上的文件的指定 FileAttributes。
public:
static void SetAttributes(System::String ^ path, System::IO::FileAttributes fileAttributes);
public static void SetAttributes (string path, System.IO.FileAttributes fileAttributes);
static member SetAttributes : string * System.IO.FileAttributes -> unit
Public Shared Sub SetAttributes (path As String, fileAttributes As FileAttributes)
参数
- path
- String
文件的路径。
- fileAttributes
- FileAttributes
枚举值的按位组合。
例外
.NET Framework 2.1 以前的 .NET Core 版本:为空、仅包含 path
空格、包含无效字符或文件属性无效。
指定的路径和/或文件名超过了系统定义的最大长度。
path
的格式无效。
指定的路径无效(例如,它位于未映射的驱动器上)。
找不到该文件。
示例
下面的示例通过向文件应用 和 属性来演示 GetAttributes
SetAttributes
和 Archive
Hidden
方法。
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;
}
}
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
注解
path
允许 参数指定相对或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory 。
可以组合某些文件属性,如 Hidden ReadOnly 和 。 其他属性(如 Normal )必须单独使用。
无法使用 方法更改 对象的 File 压缩 SetAttributes 状态。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。