File.SetAttributes 方法

定義

多載

SetAttributes(SafeFileHandle, FileAttributes)

設定與相關聯的fileHandle檔案或目錄的指定FileAttributes

SetAttributes(String, FileAttributes)

在指定路徑上設定檔案的指定 FileAttributes

SetAttributes(SafeFileHandle, FileAttributes)

來源:
File.cs
來源:
File.cs
來源:
File.cs

設定與相關聯的fileHandle檔案或目錄的指定FileAttributes

public:
 static void SetAttributes(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle, System::IO::FileAttributes fileAttributes);
public static void SetAttributes (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle, System.IO.FileAttributes fileAttributes);
static member SetAttributes : Microsoft.Win32.SafeHandles.SafeFileHandle * System.IO.FileAttributes -> unit
Public Shared Sub SetAttributes (fileHandle As SafeFileHandle, fileAttributes As FileAttributes)

參數

fileHandle
SafeFileHandle

SafeFileHandle應設定之檔案或目錄fileAttributes的 。

fileAttributes
FileAttributes

列舉值的位元組合。

例外狀況

fileHandlenull

呼叫端沒有必要的權限。

備註

您無法使用 SetAttributes(SafeFileHandle, FileAttributes) 方法來變更 物件的壓縮狀態File

適用於

SetAttributes(String, FileAttributes)

來源:
File.cs
來源:
File.cs
來源:
File.cs

在指定路徑上設定檔案的指定 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 格式無效。

指定的路徑無效 (例如,它位於未對應的磁碟機上)。

找不到檔案。

path 指定了唯讀的檔案。

-或-

這個作業在目前平台不受支援。

-或-

path 指定了目錄。

-或-

呼叫端沒有必要的權限。

範例

下列範例示範 GetAttributesSetAttributes 方法,方法是將 和 Hidden 屬性套用Archive至檔案。

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

備註

允許 path 參數指定相對路徑或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前的工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory

可以合併某些檔案屬性,例如 HiddenReadOnly。 其他屬性,例如 Normal,必須單獨使用。

您無法使用 SetAttributes 方法來變更 物件的壓縮狀態File

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於