use this code to get the UNC Path for a mapped drive
Usage -
string myUNCPath = GetUNCPath("X:\");
//
//Dll import declarations
//
[DllImport("mpr.dll")]
private static extern int WNetGetUniversalName (string lpLocalPath,
int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize);
[DllImport("mpr", CharSet=CharSet.Auto)]
protected static extern int WNetGetUniversalName (string lpLocalPath,
int dwInfoLevel, IntPtr lpBuffer, ref int lpBufferSize);
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct UNIVERSAL_NAME_INFO
{
[MarshalAs(UnmanagedType.LPTStr)]
public string lpUniversalName;
}
//
//Constants
//
protected const int NO_ERROR = 0;
protected const int ERROR_MORE_DATA = 234;
protected const int ERROR_NOT_CONNECTED = 2250;
protected const int UNIVERSAL_NAME_INFO_LEVEL = 1;
//
//GetUNCPath function
//
public string GetUNCPath(string mappedDrive)
{
UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
int bufferSize = Marshal.SizeOf(rni);
int nRet = WNetGetUniversalName(
mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,
ref rni, ref bufferSize);
if (ERROR_MORE_DATA == nRet)
{
IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);;
try
{
nRet = WNetGetUniversalName(
mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,
pBuffer, ref bufferSize);
if (NO_ERROR == nRet)
{
rni = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffe r,
typeof(UNIVERSAL_NAME_INFO));
}
}
finally
{
Marshal.FreeHGlobal(pBuffer);
}
}
switch (nRet)
{
case NO_ERROR:
return rni.lpUniversalName;
case ERROR_NOT_CONNECTED:
//Local file-name
MessageBox.Show("Share not connected");
return string.Empty;
default:
return string.Empty;
}
return string.Empty;
}
Shak.
"Marathoner" <Ma********@discussions.microsoft.com> wrote in message
news:83**********************************@microsof t.com...
I need to get the list of logical drive letters for mapped drives as well
as the UNC or share name of the drive. I can obtain an array of the drive
letters but it is the assiciated name that I cannot obtain. Example: C$ on ISDEVL2
If this is the X drive, I can get X:\ but cannot get 'C$ on ISDEVL2'.
I have tried everything I know to do and would greatly appreciate any
assistance anyone can provide. --
Robert Hill
Senior Programmer/Analyst
Wake Forest Univ Baptist Med Ctr