2009-11-09 124 views
0

我試圖使用WNetAddCOnnection2映射驅動器,但有些事情不太正確。我使用的代碼從pinvoke.net開始似乎工作。如果我單步執行代碼,我會得到一個0作爲響應,並且我可以使用System.IO.Directory.GetFiles()來檢查新的映射驅動器,這使我相信證書是正確的。無法訪問使用WNetAddConnection2添加映射驅動器

問題是驅動器在應用程序之外不可用。當我從命令提示符下鍵入net use我看到這樣列出的驅動:

Unavailable L:  \\<server>\<share>   Microsoft Windows Network 

當我嘗試訪問該驅動器我得到兩種:

The system cannot find the drive specified. 

The system cannot find the path specified. 

任何幫助將不勝感激。

下面的代碼的問題概括地說:

NETRESOURCE res = new NETRESOURCE(); 
res.iScope = RESOURCE_GLOBALNET; 
res.iType = RESOURCETYPE_DISK; 
res.iDisplayType = RESOURCEDISPLAYTYPE_SHARE; 
res.iUsage = RESOURCEUSAGE_CONNECTABLE; 
res.sRemoteName = share; 
res.sLocalName = drive; 
res.sProvider = null; 

int iFlags = 0; 
iFlags = CONNECT_UPDATE_PROFILE; 

int iResult = WNetAddConnection2(ref res, psPassword, psUsername, iFlags); 

iResult最終總是等於0。

回答

1

MSDN文章,可能幫助:
* WNetAddConnection2 - [HTTP:// msdn.microsoft.com/en-us/library/aa385413%28VS.85%29.aspx][1]
* NETRESOURCE - [http://msdn.microsoft.com/en-us/library/aa385353%28VS .85%29.aspx] [2]

我認爲你的問題是「res.iDisplayType = RESOURCEDISPLAYTYPE_SHARE」的顯示類型。也許嘗試更改爲值「0」(RESOURCEDISPLAYTYPE_GENERIC)。因此,舉例來說,我一般使用映射驅動器出現:


 
With Res 
    .dwScope = RES_SCOPE_GLOBALNET 'value 2 
    .dwType = RES_TYPE_DISK 'value of 1 
    .dwUsage = RES_USE_CONNECT 'value of 1 
    .localName = "x:" 'leave blank for no drive 
    .RemoteName = "\\\" 
End With 
lRes = WNetAddConnection2(Res, sPassword, sDomain & "\" & sPassword, RES_CNN_UPDATE_PROFILE) 
If lRes = 0 Then 
    'Success 
Else 
    'Error 
End If 

始終&前檢查從命令提示符下調用後您的連接:

1A)從系統進行連接,列表當前連接:

 
    net use 

1b)從連接的系統,列表curr耳鼻喉科會議:

 
    net session 

要斷開會話,使用API​​ 'WNetCancelConnection2',從上面下面我的代碼:


 
sServer = "\\\" 
lRes = WNetCancelConnection2(sServer, RES_CNN_UPDATE_PROFILE, True) 
If lRes `> 0 Then 
    'Success 
Else 
    'Error 
End If 

或者,只是簡單地提出使用 'net' 的命令連接:

1)要映射驅動器號:

 
    net use `: \\`\` /user:`\` ` 

2)要映射的IPC連接:

 
    net use \\`\` /user:`\` ` 

使用斷開 'net' 的命令:

1)斷開映射的驅動器:

 
    net use `: /delete 

2)斷開服務器共享:

 
    net use \\`\` /delete