![](/img/trans.png)
[英]C# Exception - File cannot be accessed because it is being used by another process
[英]The file cannot be accessed because it is being used by another process c#
我编写了Windows服务,将CSV文件从原始位置复制到新位置。
之后,已读取新位置中的CSV并将数据写入MySQL。
由于文件移动服务会触发错误,因此上述任务曾经作为2个单独的服务运行:
{“进程无法访问文件'C:\\ data.csv',因为它正在被另一个进程使用。”}
因此,我决定将2个服务合并为1个,但仍然遇到相同的问题。
我的代码如下。
程式
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:\data.csv"))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(job_reference,status)"
+ "VALUES (?jobNo, ?strClientName)";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText= querynew;
cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value = (values[0]);
cmd.Parameters.Add("?strClientName", MySqlDbType.VarChar).Value =(values[1]);
cmd.ExecuteNonQuery();
56 filemove(); <-- error trigger line
}
}
this.CloseConnection();
}
//文件移动功能
public void filemove()
{
string fileName = "data.csv";
string sourcePath = @"\\Data\Company Files\";
string targetPath = @"C:";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
//I won't include the whole code in this method since it's too lengthy.
}
Service.cs
我没有在方法中包括其余代码。
void timer1_Tick(object sender, ElapsedEventArgs e)
{
dbConnect.Truncate();
dbConnect.Insert();
dbConnect.filemove();
}
protected override void OnStart(string[] args)
{
dbConnect.Insert();
dbConnect.filemove();
}
该错误在Insert()方法内的第56行中触发。
首先关闭您的连接,然后致电。
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:\data.csv"))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(job_reference,status)"
+ "VALUES (?jobNo, ?strClientName)";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText= querynew;
cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value = (values[0]);
cmd.Parameters.Add("?strClientName", MySqlDbType.VarChar).Value =(values[1]);
cmd.ExecuteNonQuery();
}
}
this.CloseConnection();
filemove();
}
目前尚不清楚filemove()方法中发生了什么,以触发错误,这可能在未包含的部分中进一步降低了。 话虽如此,您可以尝试在StreamReader的using块之后移动对filemove()的调用(如下所示)。 将其放在using块中,StreamReader仍将打开文件,并且可能会限制filemove()中的操作。
public void Insert()
{
if (this.OpenConnection() == true)
{
using(var reader = new StreamReader(@"C:\data.csv"))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(job_reference,status)"
+ "VALUES (?jobNo, ?strClientName)";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText= querynew;
cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value = values[0]);
cmd.Parameters.Add("?strClientName", MySqlDbType.VarChar).Value = (values[1]);
cmd.ExecuteNonQuery();
}
}
filemove(); // move this here after the using block
this.CloseConnection();
}
}
当您打开文件进行读写时,您需要确保已打开文件以与其他进程共享。 有一个名为FileShare的枚举! 尝试这个:
using (var stream = new FileStream(@"C:\data.csv",FileMode.Open, FileAccess.Read,
FileShare.ReadWrite)
{
using(var reader = new StreamReader(stream))
{
List<string> listA = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
string querynew = "INSERT INTO new_jobs"
+ "(job_reference,status)"
+ "VALUES (?jobNo, ?strClientName)";
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText= querynew;
cmd.Parameters.Add("?jobNo", MySqlDbType.VarChar).Value =
(values[0]);
cmd.Parameters.Add("?strClientName", MySqlDbType.VarChar).Value =(values[1]);
cmd.ExecuteNonQuery();
56 filemove(); <-- error trigger line
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.