I am using c# for FTP file upload using this code
while(true)
{
if(condition == true)
{
Process[] proc = Process.GetProcessName("ThirdPartyApp");
if(proc.Length <0 )
{
var file = Process.GetProcess().Where(pr=>pr.ProcessName == "ThirdPartyApp")
foreach(var process in file)
{
process.kill(); // Third party application stopped
}
if(File.Exists(filename))
{
using (System.Net.WebClient client = new System.Net.WebClient())
{
client.Credentials = new System.Net.NetworkCredential("username", "password");
client.UploadFile(ftpServer + new FileInfo(filename).Name, "STOR", filename);
}
File.Delete(filename);
Process.Start("ThirdPartyApp");
}
}
}
}
My program continuously runs. At a specified time, it uploads a file to the server. After my program starts, it uploads the file successfully to the server at first time, but at another time interval, at the second time, it gives this exception.
I dont understand why it is giving error at the first loop run, but why it is giving error at the second time in loop.
The process cannot access the file 'E:\TYV.csv' because it is being used by another process.
After deleting the file, a new file immediately gets created. At the second run, which process is using that file? Is it my own application ?
Why it is not locked at the first time ?
Thanks