2013-07-21 95 views
2

我打電話給NetApi32.dll方法NetUseAdd
這就是:NetUseAdd返回錯誤67或87

USE_INFO_2 useinfo = new USE_INFO_2(); 

useinfo.ui2_remote = UNCPath; 
useinfo.ui2_username = User; 
useinfo.ui2_domainname = Domain; 
useinfo.ui2_password = Password; 
useinfo.ui2_asg_type = 0; 
useinfo.ui2_usecount = 1; 
uint paramErrorIndex; 
returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex); 

useinfo.ui2_remote = \\servername\dirname調用這個返回碼67,並與useinfo.ui2_remote = \\servername\dirname\調用,當它返回碼87

當我說,它返回碼......我的意思是要麼拋出異常並且Marshal.GetLastWin32Error()返回這個錯誤碼,或者實際調用NetUseAdd返回它。

奇怪的是,與已複製文件夾的路徑調用此當此方法成功,並與子文件夾的路徑時調用失敗。

調用機器是Windows服務器2008年,而遠程是一個Linux服務器(我不知道什麼版本或分佈)。

任何想法如何成功地連接\使用遠程資源,而不用擔心子文件夾問題?

編輯

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 
internal struct USE_INFO_2 
{ 
    internal LPWSTR ui2_local; 
    internal LPWSTR ui2_remote; 
    internal LPWSTR ui2_password; 
    internal DWORD ui2_status; 
    internal DWORD ui2_asg_type; 
    internal DWORD ui2_refcount; 
    internal DWORD ui2_usecount; 
    internal LPWSTR ui2_username; 
    internal LPWSTR ui2_domainname; 
} 

[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
internal static extern NET_API_STATUS NetUseAdd(
    LPWSTR UncServerName, 
    DWORD Level, 
    ref USE_INFO_2 Buf, 
    out DWORD ParmError); 

更多提到的信息價值:我試圖使用NetUseAdd添加遠程路徑是一個巨大的存儲(7 TB)。使用Windows資源管理器進行簡單訪問需要大約3-4秒的時間才能到達,但最終會出現。

+0

顯示將值分配給'UNCPath'的線路 – rene

+1

UNCPath =「\\\\ servername \\ somedir」;' – eyal

+0

paramErrorIndex的值是什麼?當我有一個\作爲最後一個字符時,錯誤是87,它會在我的盒子上返回2。如果我使用無效的憑證,它將返回67您的linux box域加入了嗎?我的Linux服務器不是這樣,我必須輸入服務器名稱作爲域名。 – rene

回答

3

我不是我關於這一主題的專家,但我用的是一樣的東西:

@"\\10.22.15.14\C$\Inetpub\wwwroot\db\archive\" 

,我得到一個錯誤代碼87, 後來我改變的字符串,

@"\\10.22.15.14\C$\Inetpub\wwwroot\db\archive" 

和有效。也許,它可能適用於其他人。

1

我有這個相同的問題,並設法找出罪魁禍首 - 如果使用源代碼Original Article我注意到,它只會允許我連接時不作爲管理員運行Visual Studio(我的默認設置)。希望這可以幫助別人受挫!

0

我有一個類似的問題連接到Linux服務器。爲我工作的解決方案是首先使用登錄憑證手動映射來自Windows的網絡驅動器,這必須創建一些內部配置,然後允許使用NetUseAdd進行連接。我只需要映射驅動器一次。