Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 进程无法访问文件.NET_C#_.net - Fatal编程技术网

C# 进程无法访问文件.NET

C# 进程无法访问文件.NET,c#,.net,C#,.net,我需要读取一个文本文件并将其存储到数据库中。多次访问/读取文件后,突然出现此消息 进程无法访问该文件,因为另一进程正在使用该文件 我已经检查过好几次了,但没有附加其他流程。这是我的密码 // Storing the data in a data table DataTable table = new DataTable(); table.Columns.Add(new DataColumn("L_Date", typeof(DateTime))); table.Columns.Add(new

我需要读取一个文本文件并将其存储到数据库中。多次访问/读取文件后,突然出现此消息

进程无法访问该文件,因为另一进程正在使用该文件

我已经检查过好几次了,但没有附加其他流程。这是我的密码

// Storing the data in a data table
DataTable table = new DataTable();

table.Columns.Add(new DataColumn("L_Date", typeof(DateTime)));
table.Columns.Add(new DataColumn("L_Time", typeof(TimeSpan)));
table.Columns.Add(new DataColumn("L_CardID", typeof(string)));
table.Columns.Add(new DataColumn("L_Status", typeof(string)));
table.Columns.Add(new DataColumn("L_Type", typeof(string)));
table.Columns.Add(new DataColumn("L_Catatan", typeof(string)));

int i2 = 0;
string[] content = System.IO.File.ReadAllLines(e.FullPath);

foreach (string line in content)
{
    // some code
}

“我的代码编写人员”有什么问题?

使用Process Monitor(在访问文件时监视实时活动),而不是使用Process Explorer(它可以显示所有具有句柄的文件,即使这些文件当前未被访问)

转到查找/查找句柄或DLL,搜索文件,双击该文件,将在后面的窗口中选择句柄,然后右键单击并关闭该窗口。

您可以尝试:

FileStream stream = new FileStream("file.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
StreamReader reader = new StreamReader(stream);

在多用户文件系统中,虽然问题的来源未知,但在调用方法关闭文件句柄时,锁可能无法解锁


几毫秒后重试。

您是如何检查的?其他访问可能是暂时的–请尝试使用Process Monitor查看该文件。是否在其他编辑器中打开了该文件?像MSWord?是的…我用PM检查它..没有其他进程连接..可能是防病毒的。但是,由于文件系统不在应用程序的控制范围内,因此您可能需要编写代码来处理此类暂时性问题,并实现重试/跳过逻辑。你最好现在就开始写这段代码。我想这与防病毒无关,因为错误发生在不同的文件访问时间。有时,在8次更改文件内容后,会出现错误。有时仅更改3-4次内容,就会出现错误。所以它认为不是。你是对的!当我在几毫秒后重试时,该文件可以在错误出现之前再访问几次…然后呢?然后您只需重试几次;)。用塑料袋把它包起来。