如何从VB中的网络共享中复制文件以用于Windows ce应用程序

时间:2014-06-06 13:53:34

标签: vb.net windows-ce impersonation

我正在使用vb.net进行应用程序。

我需要的是从windows ce设备访问我的电脑(win 8)并将一个文件复制到windows ce设备。 我已经做到了,但我现在需要的是一种传递用户,密码和域名的方法。 我已经研究过并发现了一些使用System.Security.WindowsImpersonationContext的解决方案所以我认为类似于在windows ce应用程序中可以使用的东西。

SOrry如果你没有得到我说过的话,但我是编程新手,英语不是我的母语。

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以使用SDF中MapDrive类的Network方法。因为它非常简单,所以该方法的来源如下(我留给你把它带到VB):

public static void MapDrive(IntPtr hwnd, string netRes, string shareName, string userName, string password)
{
    NETRESOURCE NetRes = new NETRESOURCE();         
    NetRes.dwScope = RESOURCE_GLOBALNET | RESOURCE_REMEMBERED;
    NetRes.dwType = RESOURCETYPE_DISK;
    NetRes.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
    NetRes.dwUsage = RESOURCEUSAGE_CONNECTABLE;
    NetRes.lpRemoteName = Marshal2.StringToHGlobalUni(netRes);
    NetRes.lpLocalName = Marshal2.StringToHGlobalUni(shareName);
    NetRes.lpComment = IntPtr.Zero;
    NetRes.lpProvider = IntPtr.Zero;

    int ret = WNetAddConnection3(hwnd, NetRes, password, userName, 1);

    if (ret != 0)
    {
        throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString());
    }

}

private class NETRESOURCE
{
    public int dwScope;
    public int dwType;
    public int dwDisplayType;
    public int dwUsage;
    public IntPtr lpLocalName;
    public IntPtr lpRemoteName;
    public IntPtr lpComment;
    public IntPtr lpProvider;
}

[DllImport("coredll.dll")]
private static extern int WNetAddConnection3(
    IntPtr hwndOwner, 
    NETRESOURCE lpNetResource, 
    string lpPassword, 
    string lpUserName, 
    int dwFlags);

const int RESOURCE_GLOBALNET  =    0x00000002;
const int RESOURCE_REMEMBERED =    0x00000003;
const int RESOURCETYPE_DISK  =     0x00000001;
const int RESOURCEDISPLAYTYPE_SHARE     =     0x00000003;
const int  RESOURCEUSAGE_CONNECTABLE =  0x00000001;