中易网

C#对文件的共享访问问题

答案:2  悬赏:0  
解决时间 2021-02-16 08:38
xml", FileShareFileStream fs = new FileStream(@".Open.ReadWrite)
这种形式的文件共享,另一个进程读取目标文件的时候读到的内容为空呢,会不会出现一个进程正在重写目标文件:\.ReadWrite;, FileAccess, FileMode;allSubsys;C
最佳答案
你描述的问题出现在多线程的模式下面,
如果碰巧遇到这个问题它会抛出一个IOException异常
文件“XXXX”正由另一进程使用,因此该进程无法访问该文件。
全部回答
调用windows的api吧 using system; using system.collections.generic; using system.text; using system.runtime.interopservices; namespace windowsapplication1 { #region windows api enum and struct public enum error_id { error_success = 0, // success error_more_data = 234, error_no_browser_servers_found = 6118, error_invalid_level = 124, error_access_denied = 5, error_invalid_parameter = 87, 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 } 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; } #endregion /// /// 连接网络上的共享文件夹 /// public class networksharedfolder { [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].dwtype = resource_type.resourcetype_disk; share_driver[0].lplocalname = localpath; share_driver[0].lpremotename = remotepath; disconnect(localpath); int ret = wnetaddconnection2a( share_driver, password, username, 1); return ret; } public static void disconnect(string localpath) { wnetcancelconnection2a(localpath, 1, 1); } public static void test() { int ret = networksharedfolder.connect(@"\\192.168.1.2\log", @"y:", "user1", "user1"); system.console.writeline("connect ret = " + ret); } } }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
如何判断有机化合物的氧化性,还原性,比如醇
2013奔驰c260优雅排放是什么标准
韩剧我有爱人了百度云链接
中国电信城郊支局这个地址在什么地方,我要处
S在服装上代表什么
星宿门派时装
绵阳傅记钵钵鸡两个人100元够不够
求说梦者的《大圣传》TXT精校无错字无漏字的
行车记录仪视频覆盖了还能找回来吗
去海边玩了几天回来眼屎特别多
芙蓉兴盛雨荀便利店在哪里啊,我有事要去这个
头发出油、有没有什么偏方能治?
Service Temporarily Unavailable
高新区金思维幼儿园地址在什么地方,想过去办
生前何必久睡,死后自会长眠![嘿哈][嘿哈][嘿
推荐资讯
南宁东站到广西大学生就业服务中心多远
华都星公馆一期地址有知道的么?有点事想过去
怎么让电脑自动配色
从青岛去潍坊滨海经济开发区科教创新园怎么走
网上购票没有付款
舞阳县北舞渡镇第六中心小学地址在哪,我要去
食堂环境卫生的要求食堂内外注意什么
跑步适合听的音乐,节奏感强的,谢谢
加油站(云南路店)地址在哪,我要去那里办事
你好,北京中科贴针灸健康科技有限公司有叫谷
攸县凉江乡会文中学地址好找么,我有些事要过
广告业要开发票明细为宣传品的项目这可以吗?
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?