[英]Mapped network drives cannot be listed in C#
我試圖列出我的應用程序中的所有本地驅動器, DriveInfo.GetDrives
返回本地驅動器號,我也需要映射驅動器。
目前我有:C:,D:和G:(HDD),E:和F:(CD),以及S:和Z :(映射網絡驅動器)。 (是的,它們在Windows資源管理器和Total Commander中都可見。)但是只能以編程方式檢索C,D,E,F,G。
我還嘗試了Environment.GetLogicalDrives()
, GetLogicalDriveStrings (pInvoke)
, FindFirstVolume
和FindNextVolumen
(pInvoke)。 我試圖在注冊表中找到映射的驅動器列表。 沒有任何作用,只有HDD和CD字母被檢索。
沒有任何異常,直接WinAPI調用中沒有指示錯誤,現在我被卡住了。 這是某種安全設置嗎? 我看到的每個地方,人們都說DriveInfo.GetDrives
應該返回映射的驅動器。 這是真的嗎?
我正在使用Vista Home Pro。 應用程序從本地計算機運行,它也是使用Visual Studio 2008構建的。
我發布了我使用過的內容,但它很簡單,不是我做錯了的情況:
foreach (System.IO.DriveInfo di in System.IO.DriveInfo.GetDrives())
Console.WriteLine(di.Name);
結果:C:\\ D:\\ E:\\ F:\\ G:\\按任意鍵繼續。 。 。
我怎樣才能使它工作?
好的,我發現了如何在Vista上獲得斷開連接的驅動器。 不容易,但它是可行的:
首先,您需要以下WinAPI函數的pInvoke定義:
那些需要一個結構和一些枚舉。
然后你需要多次調用它們,最后,你得到列表。 這是代碼,要注意,它很長:
[DllImport("MPR.dll", CharSet = CharSet.Auto)]
static extern int WNetEnumResource(IntPtr hEnum, ref int lpcCount, IntPtr lpBuffer, ref int lpBufferSize);
[DllImport("MPR.dll", CharSet = CharSet.Auto)]
static extern int WNetOpenEnum(RESOURCE_SCOPE dwScope, RESOURCE_TYPE dwType, RESOURCE_USAGE dwUsage,
[MarshalAs(UnmanagedType.AsAny)][In] object lpNetResource, out IntPtr lphEnum);
[DllImport("MPR.dll", CharSet = CharSet.Auto)]
static extern int WNetCloseEnum(IntPtr hEnum);
public enum RESOURCE_SCOPE : uint
{
RESOURCE_CONNECTED = 0x00000001,
RESOURCE_GLOBALNET = 0x00000002,
RESOURCE_REMEMBERED = 0x00000003,
RESOURCE_RECENT = 0x00000004,
RESOURCE_CONTEXT = 0x00000005
}
public enum RESOURCE_TYPE : uint
{
RESOURCETYPE_ANY = 0x00000000,
RESOURCETYPE_DISK = 0x00000001,
RESOURCETYPE_PRINT = 0x00000002,
RESOURCETYPE_RESERVED = 0x00000008,
}
public enum RESOURCE_USAGE : uint
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
}
public enum RESOURCE_DISPLAYTYPE : uint
{
RESOURCEDISPLAYTYPE_GENERIC = 0x00000000,
RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001,
RESOURCEDISPLAYTYPE_SERVER = 0x00000002,
RESOURCEDISPLAYTYPE_SHARE = 0x00000003,
RESOURCEDISPLAYTYPE_FILE = 0x00000004,
RESOURCEDISPLAYTYPE_GROUP = 0x00000005,
RESOURCEDISPLAYTYPE_NETWORK = 0x00000006,
RESOURCEDISPLAYTYPE_ROOT = 0x00000007,
RESOURCEDISPLAYTYPE_SHAREADMIN = 0x00000008,
RESOURCEDISPLAYTYPE_DIRECTORY = 0x00000009,
RESOURCEDISPLAYTYPE_TREE = 0x0000000A,
RESOURCEDISPLAYTYPE_NDSCONTAINER = 0x0000000B
}
[StructLayout(LayoutKind.Sequential)]
public struct NetResource
{
public RESOURCE_SCOPE dwScope;
public RESOURCE_TYPE dwType;
public RESOURCE_DISPLAYTYPE dwDisplayType;
public RESOURCE_USAGE dwUsage;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
public string lpLocalName;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
public string lpRemoteName;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
public string lpComment;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)]
public string lpProvider;
}
static System.Collections.Generic.Dictionary<string, NetResource> WNetResource(object resource)
{
System.Collections.Generic.Dictionary<string, NetResource> result = new System.Collections.Generic.Dictionary<string, NetResource>();
int iRet;
IntPtr ptrHandle = new IntPtr();
try
{
iRet = WNetOpenEnum(
RESOURCE_SCOPE.RESOURCE_REMEMBERED, RESOURCE_TYPE.RESOURCETYPE_DISK, RESOURCE_USAGE.RESOURCEUSAGE_ALL,
resource, out ptrHandle);
if (iRet != 0)
return null;
int entries = -1;
int buffer = 16384;
IntPtr ptrBuffer = Marshal.AllocHGlobal(buffer);
NetResource nr;
iRet = WNetEnumResource(ptrHandle, ref entries, ptrBuffer, ref buffer);
while ((iRet == 0) || (entries > 0))
{
Int32 ptr = ptrBuffer.ToInt32();
for (int i = 0; i < entries; i++)
{
nr = (NetResource)Marshal.PtrToStructure(new IntPtr(ptr), typeof(NetResource));
if (RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER == (nr.dwUsage
& RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER))
{
//call recursively to get all entries in a container
WNetResource(nr);
}
ptr += Marshal.SizeOf(nr);
result.Add(nr.lpLocalName, nr);
}
entries = -1;
buffer = 16384;
iRet = WNetEnumResource(ptrHandle, ref entries, ptrBuffer, ref buffer);
}
Marshal.FreeHGlobal(ptrBuffer);
iRet = WNetCloseEnum(ptrHandle);
}
catch (Exception)
{
}
return result;
}
public static System.Collections.Generic.Dictionary<string, NetResource> WNetResource()
{
return WNetResource(null);
}
你必須調用WNetResource(),然后你將獲得驅動器列表。 (和蛋糕:-))
Environment.GetLogicalDrives()
和DriveInfo.GetDrives()
都返回了我所有的網絡驅動器。
您的應用程序是否以不同的用戶身份運行(例如asp.net網站)? 如果是,是否實際為該用戶映射了驅動器? 您可能會發現驅動器已為您映射,但它們實際上並未針對您的應用程序運行的用戶進行映射。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.