![](/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.