선언:

C#

[DllImport("kernel32")]
public static extern IntPtr OpenFile(String lpFileName, ref OFSTRUCT lpReOpenBuff, UInt32 uStyle);


VB.NET

<DllImport("kernel32")> _
Public Shared Function OpenFile(lpFileName As [String], ByRef lpReOpenBuff As OFSTRUCT, uStyle As UInt32) As IntPtr
End Function




사용 예제:

C#

using System;
using System.Runtime.InteropServices;
namespace ApiReference {
    class ApiExample {
        public struct OFSTRUCT {
            public Byte cBytes;
            public Byte fFixedDisk;
            public UInt16 nErrCode;
            public UInt16 Reserved1;
            public UInt16 Reserved2;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
            public String szPathName;
        }
        [DllImport("kernel32")]
        public static extern IntPtr OpenFile(String lpFileName, ref OFSTRUCT lpReOpenBuff, UInt32 uStyle);
        [DllImport("kernel32")]
        public static extern void CloseHandle(IntPtr hObject);
        
        public static void Main(string[] args) {
            
            // 파일을 열기 위해서 구조체를 초기화한다.
            OFSTRUCT ofs = default(OFSTRUCT);
            ofs.cBytes = (Byte) Marshal.SizeOf(typeof(OFSTRUCT));
            
            // 파일을 열고
            IntPtr hFile = OpenFile(@"C:\windows\explorer.exe", ref ofs, 0);
            
            // 파일을 닫는다.
            CloseHandle(hFile);
            
            // 파일 정보를 출력한다.
            Console.WriteLine("파일 위치: {0}", ofs.szPathName);
            
            Console.ReadKey(true);
        }
    }
}


VB.NET

Imports System
Imports System.Runtime.InteropServices
Namespace ApiReference
    Class ApiExample
        Public Structure OFSTRUCT
            Public cBytes As [Byte]
            Public fFixedDisk As [Byte]
            Public nErrCode As UInt16
            Public Reserved1 As UInt16
            Public Reserved2 As UInt16
            <MarshalAs(UnmanagedType.ByValTStr, SizeConst := 128)> _
            Public szPathName As [String]
        End Structure
        <DllImport("kernel32")> _
        Public Shared Function OpenFile(lpFileName As [String], ByRef lpReOpenBuff As OFSTRUCT, uStyle As UInt32) As IntPtr
        End Function
        <DllImport("kernel32")> _
        Public Shared Sub CloseHandle(hObject As IntPtr)
        End Sub

        Public Shared Sub Main(args As String())

            ' 파일을 열기 위해서 구조체를 초기화한다.
            Dim ofs As OFSTRUCT = Nothing
            ofs.cBytes = CType(Marshal.SizeOf(GetType(OFSTRUCT)), [Byte])

            ' 파일을 열고
            Dim hFile As IntPtr = OpenFile("C:\windows\explorer.exe", ofs, 0)

            ' 파일을 닫는다.
            CloseHandle(hFile)

            ' 파일 정보를 출력한다.
            Console.WriteLine("파일 위치: {0}", ofs.szPathName)

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




예제 실행 결과:




매개 변수 설명:

lpFileName - 열 파일의 이름을 입력합니다.

lpReOpenBuff - 파일의 정보가 저장될 OFSTRUCT 구조체의 변수를 입력합니다.

uStyle - 어떻게 열 것인지를 지정하는 값을 입력합니다.




API 설명:

파일을 엽니다.




참고:

OpenFile (MSDN)




요구 사항:

Windows XP 이상




비고:

파일의 정보가 저장될 OFSTRUCT 구조체의 cBytes 멤버는 OFSTRUCT 구조체의 크기로 설정되어야 합니다.

uStyle 멤버의 경우 OpenFile API의 MSDN 문서 중 uStyle 매개 변수 부분을 참고하시기 바랍니다.



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

47. EmptyWorkingSet  (1) 2014.11.12
46. GetFileSize  (0) 2014.11.09
44. OFSTRUCT  (0) 2014.11.09
43. GetFileAttributes  (0) 2014.11.06
42. FileAttributes  (0) 2014.11.05

+ Recent posts