File.SetAttributes 方法

设置指定路径上文件的指定的 FileAttributes

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Sub SetAttributes ( _
    path As String, _
    fileAttributes As FileAttributes _
)
用法
Dim path As String
Dim fileAttributes As FileAttributes

File.SetAttributes(path, fileAttributes)
public static void SetAttributes (
    string path,
    FileAttributes fileAttributes
)
public:
static void SetAttributes (
    String^ path, 
    FileAttributes fileAttributes
)
public static void SetAttributes (
    String path, 
    FileAttributes fileAttributes
)
public static function SetAttributes (
    path : String, 
    fileAttributes : FileAttributes
)

参数

  • path
    该文件的路径。
  • fileAttributes
    所需的 FileAttributes,例如 HiddenReadOnlyNormalArchive

异常

异常类型 条件

ArgumentException

path 为空、只包含空白、包含无效字符或文件属性无效。

PathTooLongException

指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。

NotSupportedException

path 的格式无效。

DirectoryNotFoundException

指定的路径无效(例如,它位于未映射的驱动器上)。

FileNotFoundException

无法找到该文件。

UnauthorizedAccessException

path 指定了一个只读文件。

- 或 -

在当前平台上不支持此操作。

- 或 -

path 指定了一个目录。

- 或 -

调用方没有所要求的权限。

备注

允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory

无法使用 SetAttributes 方法来更改 File 对象的压缩状态。

有关使用此方法的示例,请参见“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

向文件中追加文本。

如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText

重命名或移动文件。

File.Move

FileInfo.MoveTo

获取文件属性。

File.GetAttributes

读取二进制文件。

如何:对新建的数据文件进行读取和写入

写入二进制文件。

如何:对新建的数据文件进行读取和写入

示例

下面的示例通过将 ArchiveHidden 属性应用于文件,演示了 GetAttributesSetAttributes 方法。

Imports System
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

        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";

        // 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, 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";
   
   // 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, 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";

        // Create the file if it does not exist.
        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 安全性

平台

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

请参见

参考

File 类
File 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本