首页 > 代码库 > 网络共享做本地映射

网络共享做本地映射

解决向网络共享目录存放文件的权限问题。

 1 public class WNetHelper 2     { 3         [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")] 4         private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags); 5         [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")] 6         private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce); 7  8         [StructLayout(LayoutKind.Sequential)] 9         public class NetResource10         {11             public int dwScope;12 13             public int dwType;14 15             public int dwDisplayType;16 17             public int dwUsage;18 19             public string lpLocalName;20 21             public string lpRemoteName;22 23             public string lpComment;24 25             public string lpProvider;26         }27 28         /// <summary>  29         /// 为网络共享做本地映射  30         /// </summary>  31         /// <param name="username">访问用户名(windows系统需要加计算机名,如:comp-1/user-1)</param>  32         /// <param name="password">访问用户密码</param>  33         /// <param name="remoteName">网络共享路径(如://192.168.0.9/share)</param>  34         /// <param name="localName">本地映射盘符</param>  35         /// <returns></returns>  36         public static uint WNetAddConnection(string username, string password, string remoteName, string localName)37         {38             NetResource netResource = new NetResource();39 40             netResource.dwScope = 2;41             netResource.dwType = 1;42             netResource.dwDisplayType = 3;43             netResource.dwUsage = 1;44             netResource.lpLocalName = localName;45             netResource.lpRemoteName = remoteName.TrimEnd(/);46             uint result = WNetAddConnection2(netResource, password, username, 0);47 48             return result;49         }50 51         public static uint WNetCancelConnection(string name, uint flags, bool force)52         {53             uint nret = WNetCancelConnection2(name, flags, force);54 55             return nret;56         }57 58     }

 

网络共享做本地映射