[愚翁专栏]如何把网络共享目录映射为本地驱动器

如何把网络共享目录映射为本地驱动器 <script language="javascript" type="text/javascript">document.title="如何把网络共享目录映射为本地驱动器 - "+document.title</script>

要映射一个网络目录为本地驱动器,需要调用系统DLLWNetAddConnection2函数来进行添加。

 

首先,系统函数的申明如下:

using System.Runtime.InteropServices;

 

    [DllImport("mpr.dll", EntryPoint="WNetAddConnection2")]

    public static extern uint WNetAddConnection2(

[In] NETRESOURCE lpNetResource,

string lpPassword,

string lpUsername,

uint dwFlags);

 

    [DllImport("Mpr.dll")]

    public static extern uint WNetCancelConnection2(

string lpName,

uint dwFlags,

bool fForce);

 

    [StructLayout(LayoutKind.Sequential)]

    public class NETRESOURCE

    {   

        public int dwScope;   

        public int dwType;

        public int dwDisplayType;   

        public int dwUsage;   

        public string LocalName;   

        public string RemoteName;   

        public string Comment;   

        public string Provider;

    }

 

    添加映射网络驱动器调用的代码如下:

    NETRESOURCE myNetResource = new NETRESOURCE();       

    myNetResource.dwScope = 2;       //2:RESOURCE_GLOBALNET           

    myNetResource.dwType = 1 ;       //1:RESOURCETYPE_ANY            

    myNetResource.dwDisplayType = 3; //3:RESOURCEDISPLAYTYPE_GENERIC          

    myNetResource.dwUsage = 1;       //1: RESOURCEUSAGE_CONNECTABLE    

    myNetResource.LocalName = "T:";       

    myNetResource.RemoteName = yourNetworkPath;       

    myNetResource.Provider = null;       

 

    uint nret = WNetAddConnection2( myNetResource, pwd, username, 0);

    注意:如果正确,返回值是0;否则错误。

 

    删除映射网络驱动器调用的代码如下:

    uint nret = WNetAddConnection2( yourNetDriveName, 1, true);



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=631309

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值