473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Logical Drives

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
Nov 16 '05 #1
2 9807
Use this code to get UNC path for any given mapped drive letter.

Usage -
string myfullpath = GetUNCPath("X:\");
//
//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 procedure
//
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:
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

Nov 16 '05 #2
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Travis L. Alltop | last post by:
All, To make a long story short, we are swapping out the "knock-off" drives that the NA purchased on E-Bay in one of our production SQL Servers (SQL Server 2000 Enterprise) this weekend for...
0
by: chris6995 | last post by:
I'm looking for a way to programatically query for the association between a physical device (i.e. removable USB storage) and it's logical drive letter. I've gone through Win32_LogicalDisk,...
36
by: Ron Johnson | last post by:
http://hardware.devchannel.org/hardwarechannel/03/10/20/1953249.shtml?tid=20&tid=38&tid=49 -- ----------------------------------------------------------------- Ron Johnson, Jr....
2
by: Al Sav | last post by:
Hi, After enumerating the logical drives, using GetLogicalDrives, I need to know the Drive Types, ie. Floppy or Network. Is it possible in C# to do this? Thanks in advance. Alwin S.
1
by: benoit laude | last post by:
Hello, I want to compare the path of a file with a value in a database, to see if the file is the "good" file. It seems simple!! But it doesn't work if we create a logical drive on the client...
0
by: benoit laude | last post by:
Hello, I want to compare the path of a file with a value in a database, to see if the file is the "good" file. It seems simple!! But it doesn't work if we create a logical drive on the client...
2
by: ImOk | last post by:
WHen I run this under Windows I get the Count of drives but then I get an error message on the Reset() when I try to enumerate. Does this work for anyone? $fs = new...
2
by: Bruce Cleaver | last post by:
Hello, I've been trying to find a way to enumerate all drives- logical and virtual starting at the desktop for an explorer clone. I know how to get the logical drives and icons, but have not...
0
by: dgrundel | last post by:
Hello, I've done some searching on this and the only way I can tell to get a list of logical drives and their type is using System.Management and querying Win32_LogicalDisk. While that does...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.