C#访问网络共享文件夹的方法
程序员文章站
2023-12-18 08:53:34
本文实例为大家分享了c#访问网络共享文件夹的具体代码,供大家参考,具体内容如下
using system.runtime.interopservices;...
本文实例为大家分享了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); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。