zoukankan      html  css  js  c++  java
  • 网络共享做本地映射

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Runtime.InteropServices;
     6 
     7 namespace WebApplication1
     8 {
     9     public class WNetHelper
    10     {
    11         #region Windows API函数
    12         /*
    13          * Windows API函数WNetAddConnection2
    14          * 其作用是创建一个同网络资源的连接,当调用成功时函数返回0。其参数说明如下:
    15          * lpNetResource:数据类型是结构NETRESOURCE,该结构中的各个字段对要连接的网络资源进行了定义,
    16          *      包括远程计算机共享文件夹的名字,以及本地系统为网络驱动器分配的盘符“X:”,等等;
    17          * lpPassword:数据类型为string,该参数是个可选参数,用来设置网络密码,
    18          *      如果该参数的值等于vbNullString,则表示采用系统当前登录用户的默认密码;
    19          *      如果是一个空字符串,则表示不需要任何密码就可访问网络资源;
    20          * lpUserName:数据类型为string,指定用于网络连接的用户名,如果该参数的值等于vbNullString,则表示使用系统当前的登录用户名;
    21          * dwFlags:数据类型为long,该参数可以设为0,也可以指定常量CONNECT_UPDATE_PROFILE(表示创建永久性的网络连接)。
    22          */
    23         [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
    24         private static extern uint WNetAddConnection2(
    25             [In] NetResource lpNetResource,
    26             string lpPassword,
    27             string lpUsername,
    28             uint dwFlags);
    29         /*Windows API函数WNetCancelConnection2
    30          * 其作用是断开一个网络连接,当调用成功时函数返回0。其参数说明如下:
    31          * lpName:数据类型是string,用来指定已连接资源的远程名称或本地名称,具体在本文中就是给远程共享文件夹分配的盘符:“X:”;
    32          * dwFlags:数据类型为long,可以取两个值,0或CONNECT_UPDATE_PROFILE,
    33          *          如果为0,而且建立的网络连接是永久性连接,则在Windows下次重新启动时仍会重新连接;
    34          * fForce:数据类型为long,如果为True,那么,即使连接的网络资源上有正在打开的文件或作业,也强制断开网络连接,
    35          *          这样就会造成数据的丢失、不完整性,建议将该参数的值设为False,如程序中所做。
    36          */
    37         [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
    38         private static extern uint WNetCancelConnection2(
    39             string lpName,
    40             uint dwFlags,
    41             bool fForce);
    42         //对要连接的网络资源进行了定义,包括远程计算机共享文件夹的名字,以及本地系统为网络驱动器分配的盘符“X:”,等等;
    43         [StructLayout(LayoutKind.Sequential)]
    44         public class NetResource
    45         {
    46             public int dwScope;
    47             public int dwType;
    48             public int dwDisplayType;
    49             public int dwUsage;
    50             public string lpLocalName;
    51             public string lpRemoteName;
    52             public string lpComment;
    53             public string lpProvider;
    54         }
    55 
    56         /// <summary>  
    57         /// 创建一个同网络资源的连接
    58         /// [为网络共享做本地映射]  
    59         /// </summary>  
    60         /// <param name="username">访问用户名(windows系统需要加计算机名,如:comp-1/user-1)</param>  
    61         /// <param name="password">访问用户密码</param>  
    62         /// <param name="remoteName">网络共享路径(如://192.168.0.9/share)</param>  
    63         /// <param name="localName">本地映射盘符</param>  
    64         /// <returns></returns>  
    65         public static bool WNetAddConnection(string username, string password, string remoteName, string localName)
    66         {
    67             NetResource netResource = new NetResource();
    68             netResource.dwScope = 2;
    69             netResource.dwType = 1;
    70             netResource.dwDisplayType = 3;
    71             netResource.dwUsage = 1;
    72             netResource.lpLocalName = localName;
    73             netResource.lpRemoteName = remoteName.TrimEnd('/');
    74             netResource.lpProvider = null;
    75             uint result = WNetAddConnection2(netResource, password, username, 0);
    76             return result == 0 ? true : false;
    77         }
    78         /// <summary>
    79         /// 关闭网络连接
    80         /// </summary>
    81         /// <param name="localName">映射盘符</param>
    82         /// <returns></returns>
    83         public static bool WNetCancelConnection(string localName)
    84         {
    85             uint nret = WNetCancelConnection2(localName, 1, true);
    86             return nret == 0 ? true : false;
    87         }
    88         #endregion
    89 
    90     }
    91 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 using System.IO;
     8 
     9 namespace WebApplication1
    10 {
    11     public partial class WebForm1 : System.Web.UI.Page
    12     {
    13         protected void Page_Load(object sender, EventArgs e)
    14         {
    15            bool res = WNetHelper.WNetAddConnection(@"Netnetnet-pcadmin2", @"123", @"\Netnetnet-pc站点发布",@"Y:");
    16            if (!res)
    17            {
    18                throw new Exception("连接失败");
    19            }
    20             try
    21             {
    22                 File.Copy("D:\a.txt", "Y:\b.txt", true);
    23             }
    24             catch (Exception ex)
    25             {
    26                 throw ex;
    27             }
    28         }
    29 
    30        
    31     }
    32 }
    调用
  • 相关阅读:
    对老师的评价以及课程建议
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    每日日报
    关于matlab使用medfilt2报错
    Matlab中hold on与hold off的用法
  • 原文地址:https://www.cnblogs.com/yf2011/p/3989316.html
Copyright © 2011-2022 走看看