C#映射网络磁盘
C#映射网络磁盘C#映射网络磁盘,测试通过
1.WinAPI源代码 using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; //转自http://blog.csdn.net namespace vjsdn_create_share { /// <summary> /// 映射网络磁盘.易学网 www.vjsdn.com /// </summary> public class NetwareDriveAPI { #region Windows API enum and struct public enum ERROR_ID { ERROR_SUCCESS = 0, // Success ERROR_MORE_DATA = 234, ERROR_NO_BROWSER_SERVERS_FOUND = 6118, ERROR_INVALID_LEVEL = 124, ERROR_ACCESS_DENIED = 5, ERROR_INVALID_PARAMETER = 87, ERROR_NOT_ENOUGH_MEMORY = 8, ERROR_NETWORK_BUSY = 54, ERROR_BAD_NETPATH = 53, ERROR_NO_NETWORK = 1222, ERROR_INVALID_HANDLE_STATE = 1609, ERROR_EXTENDED_ERROR = 1208 } public enum RESOURCE_SCOPE { RESOURCE_CONNECTED = 1, RESOURCE_GLOBALNET = 2, RESOURCE_REMEMBERED = 3, RESOURCE_RECENT = 4, RESOURCE_CONTEXT = 5 } public enum RESOURCE_TYPE { RESOURCETYPE_ANY = 0, RESOURCETYPE_DISK = 1, RESOURCETYPE_PRINT = 2, RESOURCETYPE_RESERVED = 8, } public enum RESOURCE_USAGE { RESOURCEUSAGE_CONNECTABLE = 1, RESOURCEUSAGE_CONTAINER = 2, RESOURCEUSAGE_NOLOCALDEVICE = 4, RESOURCEUSAGE_SIBLING = 8, RESOURCEUSAGE_ATTACHED = 16, RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED), } public enum RESOURCE_DISPLAYTYPE { RESOURCEDISPLAYTYPE_GENERIC = 0, RESOURCEDISPLAYTYPE_DOMAIN = 1, RESOURCEDISPLAYTYPE_SERVER = 2, RESOURCEDISPLAYTYPE_SHARE = 3, RESOURCEDISPLAYTYPE_FILE = 4, RESOURCEDISPLAYTYPE_GROUP = 5, RESOURCEDISPLAYTYPE_NETWORK = 6, RESOURCEDISPLAYTYPE_ROOT = 7, RESOURCEDISPLAYTYPE_SHAREADMIN = 8, RESOURCEDISPLAYTYPE_DIRECTORY = 9, RESOURCEDISPLAYTYPE_TREE = 10, RESOURCEDISPLAYTYPE_NDSCONTAINER = 11 } [StructLayout(LayoutKind.Sequential)] public struct NETRESOURCE { public RESOURCE_SCOPE dwScope; public RESOURCE_TYPE dwType; public RESOURCE_DISPLAYTYPE dwDisplayType; public RESOURCE_USAGE dwUsage; [MarshalAs(UnmanagedType.LPStr)] public string lpLocalName; [MarshalAs(UnmanagedType.LPStr)] public string lpRemoteName; [MarshalAs(UnmanagedType.LPStr)] public string lpComment; [MarshalAs(UnmanagedType.LPStr)] public string lpProvider; } #endregion [DllImport("mpr.dll")] public static extern int WNetAddConnection2A(NETRESOURCE[] lpNetResource, string lpPassword, string lpUserName, int dwFlags); [DllImport("mpr.dll")] public static extern int WNetCancelConnection2A(string sharename, int dwFlags, int fForce); //易学网 www.vjsdn.com /// <summary> /// 创建网络磁盘. 可指定对机器的访问用户名和密码 /// </summary> /// <param name="remotePath">网络路径</param> /// <param name="localPath">本地磁盘名称.如:"X:"</param> /// <param name="username">用户名</param> /// <param name="password">密码</param> /// <returns></returns> public static int Connect(string remotePath, string localPath, string username, string password) { NETRESOURCE[] share_driver = new NETRESOURCE[1]; share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK; share_driver[0].lpLocalName = localPath; share_driver[0].lpRemoteName = remotePath; Disconnect(localPath); int ret = WNetAddConnection2A(share_driver, password, username, 1); return ret; } /// <summary> /// 断开网络磁盘 /// </summary> /// <param name="localpath"></param> public static void Disconnect(string localpath) { WNetCancelConnection2A(localpath, 1, 1); } } } 2.使用 //易学网 www.vjsdn.com private void button2_Click_1(object sender, EventArgs e) { string localpath = "X:"; //本地磁盘名称 //创建网络磁盘 int status = NetwareDriveAPI.Connect(@"\\192.168.0.5\tmp", @"X:", @www.vjsdn.com, "vjsdn"); //返回 if (status == (int)vjsdn_create_share.NetwareDriveAPI.ERROR_ID.ERROR_SUCCESS) { //测试网络磁盘,写入一个文本文件 FileStream fs = new FileStream(localpath + @"\\123.txt", FileMode.OpenOrCreate); using (StreamWriter stream = new StreamWriter(fs)) { stream.WriteLine("测试网络磁盘成功!"); stream.Flush(); stream.Close(); } fs.Close(); } else { MessageBox.Show(status.ToString()); } //删除网络磁盘 NetwareDriveAPI.Disconnect(localpath); } 源代码转自csdn blog本文来源:
参考文档:
C#网络版中国象棋游戏源代码(VS2005) C#正则表达式判断url是否网络路径(http/ftp/https三种协议) C#源码-猫扑网络CRM客户关系管理系统 3.0-源码下载 C#源码-魔方动力OA办公系统McOffice 1.0-源码下载 C#源码-全能OA办公系统 2012-源码下载 C#源码-思康协同办公管理系统 5.0-源码下载 C#源码-淘特旅游网站管理系统Asp.Net版 4.3-源码下载 C#源码-雨楠(仿KFC)订餐外卖系统 7.0-源码下载 C#源码-云端CRM客户管理系统 5.28-源码下载 C#源码-智睿企业网站管理系统 v11.1.0-源码下载 C#源码-1iNethinkCMS_1.6.0-源码下载 C#源码-LebiShop网上商店系统 5.1.17 源码版-源码下载 C#源码-Dolibarr ERP CRM 3.4.2-源码下载 C#源码-OA网络办公自动化系统asp.NET-源码下载 C#源码-网络书店v1.0源码-源码下载
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |