使用C#访问网络驱动器(共享文件夹)
using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Net; namespace Phoenix.Modules.Common.Service.Uploads { public class ConnectToSharedFolder : IDisposable { readonly string _networkName; public ConnectToSharedFolder(string networkName, NetworkCredential credentials) { _networkName = networkName; var netResource = new NetResource { Scope = ResourceScope.GlobalNetwork, ResourceType = ResourceType.Disk, DisplayType = ResourceDisplayType.Share, RemoteName = networkName }; var userName = string.IsNullOrEmpty(credentials.Domain) ? credentials.UserName : $@"{credentials.Domain}\{credentials.UserName}"; var result = WNetAddConnection2( netResource, credentials.Password, userName, 0); if (result != 0) { throw new Win32Exception(result, "Error connecting to remote share"); } } ~ConnectToSharedFolder() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { WNetCancelConnection2(_networkName, 0, true); } [DllImport("mpr.dll")] private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags); [DllImport("mpr.dll")] private static extern int WNetCancelConnection2(string name, int flags, bool force); [StructLayout(LayoutKind.Sequential)] public class NetResource { public ResourceScope Scope; public ResourceType ResourceType; public ResourceDisplayType DisplayType; public int Usage; public string LocalName; public string RemoteName; public string Comment; public string Provider; } public enum ResourceScope : int { Connected = 1, GlobalNetwork, Remembered, Recent, Context }; public enum ResourceType : int { Any = 0, Disk = 1, Print = 2, Reserved = 8, } public enum ResourceDisplayType : int { Generic = 0x0, Domain = 0x01, Server = 0x02, Share = 0x03, File = 0x04, Group = 0x05, Network = 0x06, Root = 0x07, Shareadmin = 0x08, Directory = 0x09, Tree = 0x0a, Ndscontainer = 0x0b } } }
public string networkPath = @"\\{Your IP or Folder Name of Network}\Shared Data"; NetworkCredential credentials = new NetworkCredential(@"{User Name}", "{Password}"); public string myNetworkPath = string.Empty;
Upload File
public Void FileUpload(string UploadURL) { try { using (new ConnectToSharedFolder(networkPath, credentials)) { var fileList = Directory.GetDirectories(networkPath); foreach (var item in fileList) { if (item.Contains("{ClientDocument}")) { myNetworkPath = item; } } myNetworkPath = myNetworkPath + UploadURL; using (FileStream fileStream = File.Create(UploadURL, file.Length)) { await fileStream.WriteAsync(file, 0, file.Length); fileStream.Close(); } } } catch (Exception ex) { } return obj; }
上面的函数使用ConnectToSharedFolder将文件上传到网络驱动器中。对于此类,我们必须传递访问网络的网络路径,用户名和密码,然后从网络中获取特定文件夹,并使用FileStream在该文件夹内创建一个文件。
Download File
public byte[] DownloadFileByte(string DownloadURL) { byte[] fileBytes = null; using (new ConnectToSharedFolder(networkPath, credentials)) { var fileList = Directory.GetDirectories(networkPath); foreach (var item in fileList) { if (item.Contains("ClientDocuments")) { myNetworkPath = item; } } myNetworkPath = myNetworkPath + DownloadURL; try { fileBytes = File.ReadAllBytes(myNetworkPath); } catch (Exception ex) { string Message = ex.Message.ToString(); } } return fileBytes; }
上面的函数通过传递网络路径以及用于访问网络驱动器的用户名和密码,使用ConnectToSharedFolder类从网络驱动器下载文件。此函数使用File.ReadAllBytes()读取文件的字节。
注意必须
添加此代码才能在网络驱动器中读取和写入文件。
添加此代码才能在网络驱动器中读取和写入文件。
using(new ConnectToSharedFolder(networkPath, credentials)) {}
浙公网安备 33010602011771号