S
samiarja
Guest
I am attempting to transfer data from a specific folder to another in my local server, by looping through each file and use
File.Move(sourceFile, DestinationFile)
and below how my code is structured (only the code where the problem arises)
using (SftpClient sftpClient = new SftpClient(host, username, password))
{
try
{
//Connect to SFTP server
foreach (var file in files)
{
if (!file.IsDirectory)
{
try
{
//Bunch of preprocessing
bool connected = sftpClient.IsConnected;
if (connected)
{
//Download files from remote server to local server
foreach (var item_db in files_path)
{
//Convert file from .db to .csv
}
string subFolderCSV2 = Path.Combine(localDirectory, "CSV1");
using (var sftp_eagleio = new SftpClient(host_eagleio, username_eagleio, password_eagleio))
{
//Connect to Eagle.io server
foreach (var file_eagleio in files_eagleio)
{
string remoteFileName_eagleio = file_eagleio;
using (Stream file1_eagleio = new FileStream(remoteFileName_eagleio, FileMode.Open))
{
//Transfer file from the local server to Eagle.io server on the cloud
}
}
}
sftpClient.Disconnect();
sftpClient.Connect();
if (removedb == "1")
{
sftpClient.DeleteFile(remotePath);
Console.WriteLine("File removed:" + remotePath);
}
else
{
sftpClient.RenameFile(remotePath, remoteUploadedPath);
Console.WriteLine("File moved to uploaded");
}
sftpClient.Disconnect();
}
}
catch (Exception er1)
{
//MessageBox.Show(er1.Message);
}
}
}
}
catch (Exception entry)
{
Console.WriteLine(entry.Message);
}
finally
{
try
{
Console.WriteLine("Start");
string[] LocalFileDir = Directory.GetFiles(sourcePath, "*.db");
foreach (string f in LocalFileDir)
{
string fileName = Path.GetFileName(f);
string destFile = Path.Combine(targetPath, fileName);
if (removedownloaded == "1")
{
File.Delete(f);
Console.WriteLine("File Deleted" + f);
}
else
{
File.Move(f, destFile);
Console.WriteLine("File moved: " + f + "to " + destFile);
}
}
}
catch (Exception entry1)
{
Console.WriteLine(entry1.Message);
}
string[] LocalFileDirCSV = Directory.GetFiles(targetCSVPath, "*.csv");
foreach (string fCSV in LocalFileDirCSV)
{
if (removecsv == "1")
{
File.Delete(fCSV);
Console.WriteLine("File Deleted" + fCSV);
}
else
{
try
{
string fileNameCSV = Path.GetFileName(fCSV);
string destFileCSV = Path.Combine(targetCSVPathAfterTransfer, fileNameCSV);
File.Move(fCSV, destFileCSV);
Console.WriteLine("File moved: " + fCSV + "to " + destFileCSV);
}
catch (Exception entry2)
{
Console.WriteLine(entry2.Message);
}
}
}
}
}
However, when It iterates through the data in this section of the code (in this case removedownloaded is zero so it go over File.Move(....)).
):
if (removedownloaded == "1")
{
File.Delete(f);
Console.WriteLine("File Deleted" + f);
}
else
{
File.Move(f, destFile);
Console.WriteLine("File moved: " + f + "to " + destFile);
}
and before it complete the operation I receive this error on the last file:
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
The process cannot access the file because it is being used by another process.
I am not sure where it getting process somewhere else?
Any idea how to solve this issue? I appreciate that
Sami Arja
Continue reading...
File.Move(sourceFile, DestinationFile)
and below how my code is structured (only the code where the problem arises)
using (SftpClient sftpClient = new SftpClient(host, username, password))
{
try
{
//Connect to SFTP server
foreach (var file in files)
{
if (!file.IsDirectory)
{
try
{
//Bunch of preprocessing
bool connected = sftpClient.IsConnected;
if (connected)
{
//Download files from remote server to local server
foreach (var item_db in files_path)
{
//Convert file from .db to .csv
}
string subFolderCSV2 = Path.Combine(localDirectory, "CSV1");
using (var sftp_eagleio = new SftpClient(host_eagleio, username_eagleio, password_eagleio))
{
//Connect to Eagle.io server
foreach (var file_eagleio in files_eagleio)
{
string remoteFileName_eagleio = file_eagleio;
using (Stream file1_eagleio = new FileStream(remoteFileName_eagleio, FileMode.Open))
{
//Transfer file from the local server to Eagle.io server on the cloud
}
}
}
sftpClient.Disconnect();
sftpClient.Connect();
if (removedb == "1")
{
sftpClient.DeleteFile(remotePath);
Console.WriteLine("File removed:" + remotePath);
}
else
{
sftpClient.RenameFile(remotePath, remoteUploadedPath);
Console.WriteLine("File moved to uploaded");
}
sftpClient.Disconnect();
}
}
catch (Exception er1)
{
//MessageBox.Show(er1.Message);
}
}
}
}
catch (Exception entry)
{
Console.WriteLine(entry.Message);
}
finally
{
try
{
Console.WriteLine("Start");
string[] LocalFileDir = Directory.GetFiles(sourcePath, "*.db");
foreach (string f in LocalFileDir)
{
string fileName = Path.GetFileName(f);
string destFile = Path.Combine(targetPath, fileName);
if (removedownloaded == "1")
{
File.Delete(f);
Console.WriteLine("File Deleted" + f);
}
else
{
File.Move(f, destFile);
Console.WriteLine("File moved: " + f + "to " + destFile);
}
}
}
catch (Exception entry1)
{
Console.WriteLine(entry1.Message);
}
string[] LocalFileDirCSV = Directory.GetFiles(targetCSVPath, "*.csv");
foreach (string fCSV in LocalFileDirCSV)
{
if (removecsv == "1")
{
File.Delete(fCSV);
Console.WriteLine("File Deleted" + fCSV);
}
else
{
try
{
string fileNameCSV = Path.GetFileName(fCSV);
string destFileCSV = Path.Combine(targetCSVPathAfterTransfer, fileNameCSV);
File.Move(fCSV, destFileCSV);
Console.WriteLine("File moved: " + fCSV + "to " + destFileCSV);
}
catch (Exception entry2)
{
Console.WriteLine(entry2.Message);
}
}
}
}
}
However, when It iterates through the data in this section of the code (in this case removedownloaded is zero so it go over File.Move(....)).
):
if (removedownloaded == "1")
{
File.Delete(f);
Console.WriteLine("File Deleted" + f);
}
else
{
File.Move(f, destFile);
Console.WriteLine("File moved: " + f + "to " + destFile);
}
and before it complete the operation I receive this error on the last file:
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
The process cannot access the file because it is being used by another process.
I am not sure where it getting process somewhere else?
Any idea how to solve this issue? I appreciate that
Sami Arja
Continue reading...