선언:

C#

[DllImport("kernel32")]
public static extern Int32 GetFileAttributes(String FileName);


VB.NET

<DllImport("kernel32")> _
Public Shared Function GetFileAttributes(FileName As [String]) As Int32
End Function




사용 예제:

C#

using System;
using System.Runtime.InteropServices;

namespace ApiReference {
    class Program {
        public enum FileAttributes {
            ReadOnly = 0x1,
            Hidden = 0x2,
            System = 0x4,
            Directory = 0x10,
            Archive = 0x20,
            Device = 0x40,
            Normal = 0x80,
            Temporary = 0x100,
            SparseFile = 0x200,
            ReparsePoint = 0x400,
            Compressed = 0x800,
            Offline = 0x1000,
            ContentNotIndexed = 0x2000,
            Encrypted = 0x4000,
            IntegrityStream = 0x8000,
            Virtual = 0x10000,
            NoScrubData = 0x20000,
        }
        [DllImport("kernel32")]
        public static extern Int32 GetFileAttributes(String FileName);
        
        public static void Main(string[] args) {
            String fileName;
            while ( true ) {
                Console.Write("특성을 가져올 파일의 위치를 입력하세요!\n: ");
                fileName = Console.ReadLine().Trim();
                if ( !String.IsNullOrEmpty(fileName) )
                    break;
            };
            Int32 attr = GetFileAttributes(fileName);
            Console.WriteLine("\n파일에 적용된 특성:");
            
            if ( attr == -1 ){
                Console.WriteLine("파일이 없거나 파일을 읽을 수 없습니다.");
                return;
            }
            foreach ( var val in Enum.GetValues(typeof(FileAttributes)) ) {
                if ( (attr & (int) val) != 0 )
                    Console.WriteLine("  {0}", val);
            }

            Console.ReadKey(true);
        }
    }
}


VB.NET

Imports System
Imports System.Runtime.InteropServices

Namespace ApiReference
    Class Program
        Public Enum FileAttributes
            [ReadOnly] = &H1
            Hidden = &H2
            System = &H4
            Directory = &H10
            Archive = &H20
            Device = &H40
            Normal = &H80
            Temporary = &H100
            SparseFile = &H200
            ReparsePoint = &H400
            Compressed = &H800
            Offline = &H1000
            ContentNotIndexed = &H2000
            Encrypted = &H4000
            IntegrityStream = &H8000
            Virtual = &H10000
            NoScrubData = &H20000
        End Enum
        <DllImport("kernel32")> _
        Public Shared Function GetFileAttributes(FileName As [String]) As Int32
        End Function

        Public Shared Sub Main(args As String())
            Dim fileName As [String]
            While True
                Console.Write("특성을 가져올 파일의 위치를 입력하세요!" & vbLf & ": ")
                fileName = Console.ReadLine().Trim()
                If Not [String].IsNullOrEmpty(fileName) Then
                    Exit While
                End If
            End While
            

            Dim attr As Int32 = GetFileAttributes(fileName)
            Console.WriteLine(vbLf & "파일에 적용된 특성:")

            If attr = -1 Then
                Console.WriteLine("파일이 없거나 파일을 읽을 수 없습니다.")
                Return
            End If
            For Each val As var In [Enum].GetValues(GetType(FileAttributes))
                If (attr And CInt(val)) <> 0 Then
                    Console.WriteLine("  {0}", val)
                End If
            Next

            Console.ReadKey(True)
        End Sub
    End Class
End Namespace




예제 실행 결과:




매개 변수 설명:

FileName - 특성을 가져올 파일의 경로를 입력합니다.




API 설명:

파일의 특성을 가져옵니다.




참고:

GetFileAttributes (MSDN)

FileAttributes 열거형 (API Reference)




비고:

이 API의 반환 값이 파일의 특성을 나타냅니다. 반환 값이 -1인 경우엔 파일이 존재하지 않거나, 파일에 접근할 수 없기 때문입니다. 이 API는 파일 뿐만 아니라, 디렉터리 및 장치까지에도 (=드라이브) 사용이 가능합니다.



'API Reference' 카테고리의 다른 글

45. OpenFile  (0) 2014.11.09
44. OFSTRUCT  (0) 2014.11.09
42. FileAttributes  (0) 2014.11.05
41. GetTempPath  (0) 2014.10.31
40. PROCESS_BASIC_INFORMATION  (0) 2014.10.31

+ Recent posts