0

I have an app that downloads files from our server using SFTP with SSH.NET, recursively through a few folders. The download works just fine, the first time you click the download button. When the download is complete, and you try to click the download button again, an exception is thrown at the File.Create(destFilePath) saying that the process cannot access the file because it is being used by another process. I thought that by encapsulating this stream with using that it would handle the disposing properly. I can't figure out why it won't let me download a second time without closing and re-opening the app, any help would be appreciated!

public void DownloadDirectory(SftpClient sftpClient, string sourceRemotePath, string destLocalPath)
    {
        Directory.CreateDirectory(destLocalPath);
        IEnumerable<SftpFile> files = sftpClient.ListDirectory(sourceRemotePath);
        foreach (SftpFile file in files)
        {
            if (source != null)
            {
                if (source.IsCancellationRequested)
                {                     
                    sftpClient.Disconnect();
                    return;
                }
            }

            if ((file.Name != ".") && (file.Name != ".."))
            {
                string sourceFilePath = sourceRemotePath + "/" + file.Name;
                string destFilePath = Path.Combine(destLocalPath, file.Name);
                if (file.IsDirectory)
                {
                    DownloadDirectory(sftpClient, sourceFilePath, destFilePath);
                }
                else
                {                        
                    using (Stream fileStream = File.Create(destFilePath))
                    {
                        
                        SftpFileAttributes attrs = sftpClient.GetAttributes(file.FullName);
                        int max = (int)attrs.Size;
                        progressBar1.Invoke((MethodInvoker)delegate
                        {
                            progressBar1.Maximum = max;
                            label1.Text = "Downloading " + file.Name + "...";
                        });
                        sftpClient.DownloadFile(sourceFilePath, fileStream, DownloadProgressBar);
                   
                      
                    }
                  
                }
            }
        }
    }
buradd
  • 1,271
  • 1
  • 13
  • 19
  • 1
    Are you sure it's your app that is locking the file? You can try [this app](https://lockhunter.com/) or [this Q&A](https://stackoverflow.com/a/2781509/2983568) or [this one](https://stackoverflow.com/a/707339/2983568) to see if it's better. – evilmandarine Sep 23 '21 at 22:28
  • @evilmandarine the first Q&A link you posted seems promising, but the SSH.NET DownloadFile method takes a stream, not a stream writer.. any tips? – buradd Sep 24 '21 at 20:14
  • Replace `using` by `try-catch-finally` and show the *exact exception* you're getting. Put a breakpoint in the `catch` and use LockHunter or ProcessExplorer to see what is locking the file. Check the file is not read-only after it is first created. Replace `Create` with `File.Open(sourceFilePath, FileMode.OpenOrCreate, FileAccess.Write)`. Check you don't have an antivirus or BitDefender scanning or quarantining the file during second app execution. Try to manually close the stream after `DownloadFile` (although this should be done in the `Dispose`, as you said). – evilmandarine Sep 25 '21 at 10:54

0 Answers0