[英]The file cannot be accessed because it is being used by another process c#
I have written a windows service to copy a CSV file from an original location to a new location. 我编写了Windows服务,将CSV文件从原始位置复制到新位置。
Afterward, the CSV in the new location has been read and write the data to MySQL. 之后,已读取新位置中的CSV并将数据写入MySQL。
The above tasks used to be run as 2 separate services since the file moving service triggers an error: 由于文件移动服务会触发错误,因此上述任务曾经作为2个单独的服务运行:
{"The process cannot access the file 'C:\\data.csv' because it is being used by another process."}
{“进程无法访问文件'C:\\ data.csv',因为它正在被另一个进程使用。”}
Consequently, I decided to merge the 2 services into 1 but still I got the same issue. 因此,我决定将2个服务合并为1个,但仍然遇到相同的问题。
My code is as follows. 我的代码如下。
program.cs 程式
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();
}
// file move function //文件移动功能
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 Service.cs
I do not include the rest of the code inside the methods. 我没有在方法中包括其余代码。
void timer1_Tick(object sender, ElapsedEventArgs e)
{
dbConnect.Truncate();
dbConnect.Insert();
dbConnect.filemove();
}
protected override void OnStart(string[] args)
{
dbConnect.Insert();
dbConnect.filemove();
}
The error triggers in the line 56 inside Insert() method. 该错误在Insert()方法内的第56行中触发。
First close your connection and then call. 首先关闭您的连接,然后致电。
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();
}
It's not clear what's happening within the filemove() method to trigger the error, it's probably further down in the part that wasn't included. 目前尚不清楚filemove()方法中发生了什么,以触发错误,这可能在未包含的部分中进一步降低了。 That being said, you may try moving the call to filemove() after the using block of the StreamReader (as shown below).
话虽如此,您可以尝试在StreamReader的using块之后移动对filemove()的调用(如下所示)。 With it in the using block the StreamReader still has the file open and may restrict what's going on in 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();
}
}
When you open your file for reading or writing or whatever , you need to make sure it has been opened for sharing with other processes. 当您打开文件进行读写时,您需要确保已打开文件以与其他进程共享。 There is Enum called FileShare for that!
有一个名为FileShare的枚举! Try this:
尝试这个:
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.