xml", FileShareFileStream fs = new FileStream(@".Open.ReadWrite)
这种形式的文件共享,另一个进程读取目标文件的时候读到的内容为空呢,会不会出现一个进程正在重写目标文件:\.ReadWrite;, FileAccess, FileMode;allSubsys;C
C#对文件的共享访问问题
答案:2 悬赏:0
解决时间 2021-02-16 08:38
- 提问者网友:伴他一生,无悔
- 2021-02-16 02:14
最佳答案
- 二级知识专家网友:如果这是命
- 2021-02-16 03:19
你描述的问题出现在多线程的模式下面,
如果碰巧遇到这个问题它会抛出一个IOException异常
文件“XXXX”正由另一进程使用,因此该进程无法访问该文件。
如果碰巧遇到这个问题它会抛出一个IOException异常
文件“XXXX”正由另一进程使用,因此该进程无法访问该文件。
全部回答
- 1楼网友:恕我颓废
- 2021-02-16 04:32
调用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);
}
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯