File.SetAttributes(String, FileAttributes) 메서드

정의

지정된 경로에 있는 파일의 지정된 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

열거형 값의 비트 조합입니다.

예외

2.1보다 오래된 .NET Framework 및 .NET Core 버전: path 비어 있거나, 공백만 포함하거나, 잘못된 문자를 포함하거나, 파일 특성이 잘못되었습니다.

지정된 경로, 파일 이름 또는 둘 다가 시스템에서 정의한 최대 길이를 초과합니다.

path의 형식이 잘못되었습니다.

지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

파일을 찾을 수 없습니다.

path에 읽기 전용인 파일이 지정되었습니다.

또는 현재 플랫폼이 해당 작업을 지원하지 않는 경우

또는 path에 디렉터리가 지정되었습니다.

또는 호출자에게 필요한 권한이 없는 경우

예제

다음 예제에서는 파일 및 SetAttributes 특성을 적용하여 Archive 메서드와 Hidden 메서드를 보여 GetAttributes 줍니다.

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은 단독으로 사용해야 합니다.

메서드를 사용하여 SetAttributes 개체의 File 압축 상태를 변경할 수 없습니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

적용 대상

추가 정보