다음을 통해 공유


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입니다.

예외

예외 형식 조건

ArgumentException

path가 비어 있거나, 공백만 포함하거나 또는 잘못된 문자를 포함하는 경우

PathTooLongException

지정된 경로 또는 파일 이름이 시스템에 정의된 최대 길이를 초과하는 경우. 예를 들어, Windows 기반 플랫폼에서는 경로에 248자 미만의 문자를 사용해야 하며 파일 이름에는 260자 미만의 문자를 사용해야 합니다.

NotSupportedException

path의 형식이 잘못된 경우

FileNotFoundException

path가 파일을 나타내며 유효하지 않은 경우(예: 매핑되지 않은 드라이브의 경로를 지정한 경우나 파일을 찾을 수 없는 경우)

DirectoryNotFoundException

path가 디렉터리를 나타내며 유효하지 않은 경우(예: 매핑되지 않은 드라이브의 경로를 지정한 경우나 디렉터리를 찾을 수 없는 경우)

설명

path 매개 변수에는 상대 경로나 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리에 상대적으로 해석됩니다. 현재 작업 디렉터리를 얻는 방법에 대해서는 GetCurrentDirectory를 참조하십시오.

이 메서드의 사용에 대한 예제는 예제 단원을 참조하십시오. 다음 표에서는 일반적인 예 또는 관련된 I/O 작업의 예를 보여 줍니다.

수행 작업

참조 항목

텍스트 파일을 만듭니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에 씁니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에서 읽습니다.

방법: 파일의 텍스트 읽기

파일에 텍스트를 추가합니다.

방법: 로그 파일 열기 및 추가

File.AppendText

FileInfo.AppendText

파일 이름을 바꾸거나 이동합니다.

File.Move

FileInfo.MoveTo

이진 파일에서 읽습니다.

방법: 새로 만든 데이터 파일 읽기 및 쓰기

이진 파일에 씁니다.

방법: 새로 만든 데이터 파일 읽기 및 쓰기

파일 특성을 설정합니다.

SetAttributes

예제

다음 예제에서는 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"
        ' 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 보안

플랫폼

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
방법: 파일의 텍스트 읽기
방법: 파일에 텍스트 쓰기