【问题标题】:WNetUseConnection connects to remote server/UNC on same domain but not to a different/remote domainWNetUseConnection 连接到同一域上的远程服务器/UNC,但不连接到不同的/远程域
【发布时间】:2012-03-24 04:47:42
【问题描述】:

我可以连接到远程 UNC,只要它在同一个网络上,但是当我尝试跨域访问服务器时,比如从网络 A 到网络 B,它没有连接,我收到网络路径的消息没找到。如何在不同/不受信任的网络上连接远程 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 可能更适合这种事情。
  • 如果从 Windows 资源管理器手动尝试,这是否有效?
  • 我找到了答案,谢谢大家的帮助。
  • @MS Stp 您找到解决此问题的方法了吗?我遇到了同样的错误“找不到网络路径”
  • @MStp 我知道它非常古老的问题。你是怎么解决的?

标签: c#


【解决方案1】:

如果使用桌面发布,这将通过网络访问。

【讨论】:

  • 你能解释一下桌面出版吗?
猜你喜欢
  • 2017-02-24
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-16
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多