.NET/Windows API for .NET

GetShortPathName - 단축 path명 얻기

클리엘 2019. 8. 6. 15:52
728x90

GetShortPathName함수는 지정된 Full Path에서 Short Path를 가져오는 함수입니다.

Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszIntegerPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Integer) As Integer

▶VB.NET 선언

[DllImport("kernel32")]
public static extern int GetShortPathName(string lpszIntegerPath, StringBuilder lpszShortPath, int cchBuffer);

▶C# 선언


GetShortPathName함수의 첫번째 인수는 Short Path를 가져올 Full Path를 지정하며 이때 가져온 Short Paht를 저장할 변수를 두번째 인수에서 지정합니다. 세번째 인수는 두번째 인수에서 지정한 변수가 몇 Byte까지 저장할 수 있는지를 나타내면 됩니다.

예를 들어 'C:\Program Files\Windows Media Player\setup_wm.exe'의 단축 Path명을 가져오고자 한다면 GetShortPathName함수는 다음과 같이 호출될 수 있습니다.

Dim sPath As String = Space(256)
Dim iPath As Integer = 255

Call GetShortPathName("C:\Program Files\Windows Media Player\setup_wm.exe", sPath, iPath)

▶VB.NET 호출

StringBuilder sPath = new StringBuilder(256);
int iPath = 255;

GetShortPathName(@"C:\Program Files\Windows Media Player\setup_wm.exe", sPath, iPath);

▶C# 호출


이렇게 함수를 호출하고 나면 sPath변수에 단축 Path명이 들어가게 됩니다.

함수가 정상적으로 실행되었을때는 sPath에 저장한 단축 Path의 길이가 반환되고는 정상적으로 실행되지 못하였을 때는 0값 반환합니다.

728x90