C#访问网络共享文件夹的方法
本文实例为大家分享了C#访问网络共享文件夹的具体代码,供大家参考,具体内容如下
using System.Runtime.InteropServices; public enum ERROR_ID { ERROR_SUCCESS = 0, // Success ERROR_BUSY = 170, ERROR_MORE_DATA = 234, ERROR_NO_BROWSER_SERVERS_FOUND = 6118, ERROR_INVALID_LEVEL = 124, ERROR_ACCESS_DENIED = 5, ERROR_INVALID_PASSWORD = 86, ERROR_INVALID_PARAMETER = 87, ERROR_BAD_DEV_TYPE = 66, 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, ERROR_DEVICE_ALREADY_REMEMBERED = 1202, ERROR_NO_NET_OR_BAD_PATH = 1203 } 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; } public class NetworkConnection { [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); public static int Connect(string remotePath, string localPath, string username, string password) { NETRESOURCE[] share_driver = new NETRESOURCE[1]; share_driver[0].dwScope = RESOURCE_SCOPE.RESOURCE_GLOBALNET; share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK; share_driver[0].dwDisplayType = RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE; share_driver[0].dwUsage = RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE; share_driver[0].lpLocalName = localPath; share_driver[0].lpRemoteName = remotePath; Disconnect(localPath); int ret = WNetAddConnection2A(share_driver, password, username, 1); return ret; } public static int Disconnect(string localpath) { return WNetCancelConnection2A(localpath, 1, 1); } }
测试方法:
public void TestNetWorkConnection() { string localpath = "X:"; //int status = NetworkConnection.Connect(@"//192.168.0.2/test", localpath, @"test", "test"); int status = NetworkConnection.Connect("////192.168.0.2//test", localpath, @"test", "test"); if (status == (int)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 { Console.WriteLine(status); } NetworkConnection.Disconnect(localpath); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
栏 目:C#教程
下一篇:C#使用private font改变PDF文件的字体详解
本文标题:C#访问网络共享文件夹的方法
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/5153.html
您可能感兴趣的文章
- 01-10C#使用ADO.Net部件来访问Access数据库的方法
- 01-10C#访问SQL Server数据库的实现方法
- 01-10C#警惕匿名方法造成的变量共享实例分析
- 01-10详解C#中的接口属性以及属性访问器的访问限制
- 01-10在C#使用字典存储事件示例及实现自定义事件访问器
- 01-10基于C#实现网络爬虫 C#抓取网页Html源码
- 01-10利用C#实现网络爬虫
- 01-10总结C#网络编程中对于Cookie的设定要点
- 01-10使用C#实现基于TCP和UDP协议的网络通信程序的基本示例
- 01-10浅谈Visual C#进行图像处理(读取、保存以及对像素的访问)
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10SublimeText编译C开发环境设置
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 04-02jquery与jsp,用jquery
- 01-10C#中split用法实例总结
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10delphi制作wav文件的方法
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?