C#中windows访问共享文件夹会用到的代码
2022/2/14 7:13:08
本文主要是介绍C#中windows访问共享文件夹会用到的代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
转载:https://www.cnblogs.com/liao-long/p/15398546.html
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 string Name { get; set; } 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); } //一般情况下,共享文件夹是不会用用户密码的 //首先需要在cmd中注册,注册命令是:net use +共享文件夹服务器路径,代码实现这里就不贴代码了,园子里很多实现代码 //删除:net use /delete* /yes //然后调用: int status = NetworkConnection.Disconnect(@"\\172.21.45.97\IPC$"); //如果返回0,则链接成功,此时就可以操作文件了 // var data = new DirectoryInfo(@"\\172.21.45.97\CreateTmp\").GetFileSystemInfos(); //这是获取文件路径,如果共享是的盘符,则改为指定盘符则可 //这里把上传、下载代码贴出来,实测有效 /// <summary> /// 从远程服务器下载文件到本地 /// </summary> /// <param name="src">下载到本地后的文件路径,包含文件的扩展名</param> /// <param name="dst">远程服务器路径(共享文件夹路径)</param> /// <param name="fileName">远程服务器(共享文件夹)中的文件名称,包含扩展名</param> public static void TransportRemoteToLocal(string src,string dst,string fileName) { if (!Directory.Exists(dst)) { Directory.CreateDirectory(dst); } dst = dst + fileName; FileStream inFileStream = new FileStream(dst, FileMode.Open); //远程服务器文件 此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错 FileStream outFileStream = new FileStream(src, FileMode.OpenOrCreate); //从远程服务器下载到本地的文件 byte[] buf = new byte[inFileStream.Length]; int byteCount; while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0) { outFileStream.Write(buf, 0, byteCount); } inFileStream.Flush(); inFileStream.Close(); outFileStream.Flush(); outFileStream.Close(); } /// <summary> /// 将本地文件上传到远程服务器共享目录 /// </summary> /// <param name="src">本地文件的绝对路径,包含扩展名</param> /// <param name="dst">远程服务器共享文件路径,不包含文件扩展名</param> /// <param name="fileName">上传到远程服务器后的文件扩展名</param> public static void TransportLocalToRemote(string src, string dst, string fileName) //src { FileStream inFileStream = new FileStream(src, FileMode.Open); //此处假定本地文件存在,不然程序会报错 if (!Directory.Exists(dst)) //判断上传到的远程服务器路径是否存在 { Directory.CreateDirectory(dst); } dst = dst + fileName; //上传到远程服务器共享文件夹后文件的绝对路径 FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate); byte[] buf = new byte[inFileStream.Length]; int byteCount; while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0) { outFileStream.Write(buf, 0, byteCount); } inFileStream.Flush(); inFileStream.Close(); outFileStream.Flush(); outFileStream.Close(); } //TransportRemoteToLocal(@"D:\\Models\\个人介绍.pptx", @"\\192.168.1.1\Tep\", "个人介绍.pptx"); }
这篇关于C#中windows访问共享文件夹会用到的代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具