Topic "Downloading File gives an error The process cannot access th"

Author Message
OwaisAhmed

Guest


I am using Winscp to download csv file from the SFTP server. This is my code



Code:
 if (!File.Exists(FlagFilePath))
                {
                    Debug.WriteLine("Trying to download sales data file ");

                    SessionOptions sessionOptions = new SessionOptions
                    {
                        Protocol = Protocol.Sftp,
                        HostName = ConfigurationManager.AppSettings["SFTPDomain"],
                        UserName = ConfigurationManager.AppSettings["SFTPUser"],
                        Password = ConfigurationManager.AppSettings["SFTPPass"],
                        PortNumber = Convert.ToInt32(ConfigurationManager.AppSettings["SFTPPortNumber"]),
                        GiveUpSecurityAndAcceptAnySshHostKey = true,

                    };

                    using (Session session = new Session())
                    {

                        //Attempts to connect to your SFtp site
                        session.Open(sessionOptions);

                        //Get SFtp File
                        TransferOptions transferOptions = new TransferOptions();
                        transferOptions.TransferMode = TransferMode.Binary; //The Transfer Mode - Automatic, Binary, or Ascii
                        transferOptions.FilePermissions = null;  //Permissions applied to remote files;
                        transferOptions.PreserveTimestamp = false;  //Set last write time of destination file
                        //to that of source file - basically change the timestamp to match destination and source files.   
                        transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
                        //SFTP File Path
                        Sftp_RemotePath = ConfigurationManager.AppSettings["SFTPFileName"].ToString();
                        //Delete File if Exist
                           if (System.IO.File.Exists(FilePath))
                        {
                            System.IO.File.Delete(FilePath);
                        }
                        //the parameter list is: remote Path, Local Path with filename
                        TransferOperationResult transferOperationResult = session.GetFiles(Sftp_RemotePath, FilePath , false, transferOptions);
                   
                        //Throw on any error
                        transferOperationResult.Check();
                        Debug.WriteLine("Downloaded fresh sales data file!");
                    }
                }


I am using MVC so i have two controllers that are accessing this class and when i run the controller one at a time then it works fine but when i run both controllers together then i get this error in one of the controller

Code:
{WinSCP.SessionRemoteException: Can't create file 'D:\TESTING\SFTP\Data.csv'. ---> WinSCP.SessionRemoteException: System Error.
      Code: 32.
    The process cannot access the file because it is being used by another process
       --- End of inner exception stack trace ---
       at WinSCP.OperationResultBase.Check()
       at JetStarAPI.Models.SFTPClient.DownloadFile(String FilePath) in D:\TESTING\SFTP\Models\SFTPClient.cs:line 65}


I am getting this error after this line

Code:
  transferOperationResult.Check();


if i change the name of the file here

Code:
  TransferOperationResult transferOperationResult = session.GetFiles(Sftp_RemotePath, FilePath+Path.GetRandomFileName() , false, transferOptions);


It works fine and save the file with random file name but i want to pass my FileName. How to solve this?
Advertisements
Owais Ahmed

Guest


Any help???
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24995
Location: Prague, Czechia
Your duplicate question on StackOverflow:
http://stackoverflow.com/questions/27008195/file-being-used-by-another-process-error
Owais.Ahmed

Guest


yes because it did not solve my problem and i am using winScp so better chances to fix this issue from this forum !
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24995
Location: Prague, Czechia
Owais.Ahmed wrote:
yes because it did not solve my problem and i am using winScp so better chances to fix this issue from this forum !

From WinSCP perspective, there's nothing more to suggest that what was already stated on StackOverflow.
You cannot download to the same file from two processed/threads running in parallel.
Either make the destination file name unique for each process/thread or protect against concurrent access. As per example suggested by @Piyush Parashar.
_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License