■ 장치 볼륨명 구하기 예제

------------------------------------------------------------------------------------------------------------------------

using System;

 

string logicalDriveName = "C:\\";

string deviceVolumeName = GetDeviceVolumeName(logicalDriveName);

 

Console.WriteLine("논리적 드라이브명 : {0}", logicalDriveName);

Console.WriteLine("장치 볼륨명       : {0}", deviceVolumeName);

------------------------------------------------------------------------------------------------------------------------

 

■ 장치 볼륨명 구하기

------------------------------------------------------------------------------------------------------------------------

using System;

using System.Runtime.InteropServices;

using System.Text;

 

#region DOS 장치 질의하기 - QueryDosDevice(deviceName, targetPathStringBuilder, maximumCount);

 

/// <summary>

/// DOS 장치 질의하기

/// </summary>

/// <param name="deviceName">장치명</param>

/// <param name="targetPathStringBuilder">타겟 경로 문자열 빌더</param>

/// <param name="maximumCount">최대 카운트</param>

/// <returns>처리 결과</returns>

[DllImport("kernel32.dll", SetLastError = true)]

private static extern int QueryDosDevice([In] string deviceName, [Out] StringBuilder targetPathStringBuilder, [In] int maximumCount);

 

#endregion

 

/// <summary>

/// 최대 경로

/// </summary>

private const int MAXIMUM_PATH = 260;

 

#region 장치 볼륨명 구하기 - GetDeviceVolumeName(logicalDriveName)

 

/// <summary>

/// 장치 볼륨명 구하기

/// </summary>

/// <param name="logicalDriveName">논리적 드라이브명</param>

/// <returns>장치 볼륨명</returns>

public string GetDeviceVolumeName(string logicalDriveName)

{

    string deviceName = logicalDriveName.Substring(0, 2);

 

    StringBuilder targetPathStringBuilder = new StringBuilder(MAXIMUM_PATH);

 

    int result = WIN32APIHelper.QueryDosDevice(deviceName, targetPathStringBuilder, MAXIMUM_PATH);

 

    if(result == 0)

    {

        if(Marshal.GetLastWin32Error() == 5)

        {

            throw new UnauthorizedAccessException();

        }

    }

 

    return targetPathStringBuilder.ToString();

}

 

#endregion

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요