您好,登錄后才能下訂單哦!
怎么在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#是一個簡單、通用、面向對象的編程語言,它由微軟Microsoft開發,繼承了C和C++強大功能,并且去掉了一些它們的復雜特性,C#綜合了VB簡單的可視化操作和C++的高運行效率,以其強大的操作能力、優雅的語法風格、創新的語言特性和便捷的面向組件編程從而成為.NET開發的首選語言,但它不適用于編寫時間急迫或性能非常高的代碼,因為C#缺乏性能極高的應用程序所需要的關鍵功能。
看完上述內容,你們掌握怎么在C#中訪問網絡共享文件夾的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。