0
只要它位於同一網絡上,我可以連接到遠程UNC,但是當我嘗試跨域訪問服務器時,請說從網絡A到網絡B,它沒有連接,我得到的消息沒有找到網絡路徑。如何在不同/不可信網絡上連接遠程UNC。WNetUseConnection連接到同一個域上的遠程服務器/ UNC,但不連接到另一個/遠程域
[DllImport("Mpr.dll")]
private static extern int WNetUseConnection(
IntPtr hwndOwner,
NETRESOURCE lpNetResource,
string lpPassword,
string lpUserID,
int dwFlags,
string lpAccessName,
string lpBufferSize,
string lpResult
);
[DllImport("Mpr.dll")]
private static extern int WNetCancelConnection2(
string lpName,
int dwFlags,
bool fForce
);
[StructLayout(LayoutKind.Sequential)]
private class NETRESOURCE
{
public int dwScope = 0;
public int dwType = 0;
public int dwDisplayType = 0;
public int dwUsage = 0;
public string lpLocalName = null;//changed from "" to null
public string lpRemoteName = "";
public string lpComment = "";
public string lpProvider = null;//changed from "" to null
}
public static string connectToRemote(string remoteUNC, string username, string password)
{
return connectToRemote(remoteUNC, username, password, false);
}
public static string connectToRemote(string remoteUNC, string username, string password, bool promptUser)
{
NETRESOURCE nr = new NETRESOURCE();
nr.dwType = RESOURCETYPE_DISK;
nr.lpRemoteName =remoteUNC;
int ret;
if (promptUser)
ret = WNetUseConnection(IntPtr.Zero, nr, "", "", CONNECT_INTERACTIVE | CONNECT_PROMPT, null, null, null);
else
ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);
if (ret == NO_ERROR) return null;
return getErrorForNumber(ret);
}
public static string disconnectRemote(string remoteUNC)
{
int ret = WNetCancelConnection2(remoteUNC, CONNECT_UPDATE_PROFILE, false);
if (ret == NO_ERROR) return null;
return getErrorForNumber(ret);
}
FTP可能更適合這類事情。 – 2012-03-12 17:43:48
如果從Windows資源管理器手動嘗試,這是否可以工作? – Yahia 2012-03-12 17:59:47
我發現答案的人謝謝你的幫助。 – MStp 2012-03-12 18:17:50