Get file size from SFTP using SharpSSH

2020-06-25 05:29发布

问题:

I'm uploading a zip file from local directory to SFTP using SharpSSH. Everything works fine. However, I would like to get the file size of the current file I just uploaded. The reason I'm doing this is that the zip file is big (from 45 GB to 80 GB) and I want to make sure that during the upload it didn't fail or stuck. Want to make sure the entire zip is uploaded.

I can get the file size of the local zip file like this:

DirectoryInfo di = new DirectoryInfo(yesterdaysArchiveFolder);
FileInfo[] files = di.GetFiles();

foreach (FileInfo f in files)
{
   Console.WriteLine("Size of the zip file: " + f.Length);
}

Now, I want to do the same thing for the file I just uploaded to SFTP after the upload is complete.

Since I know the name of the file I just uploaded I create an ArrayList and put the files from SFTP. I then use for loop to get the file I just uploaded.

ArrayList FileList = oSftp.GetFileList(_ftpDirectory);
int count = FileList.Count;
Console.WriteLine("Files in SFTP: " + count);

for (int i = 0; i < FileList.Count; i++)
{
     if (zipFileName == FileList[i].ToString())
     {
       Console.WriteLine(FileList[i]);          
     }
}

The problem is that there're no properties like .Length to get the file size of that file?

Is there another approach I can take to find out the file size of the file in remote server?

回答1:

SharpSSH does not allow that. But you can code it as SharpSSH is open source.

See my answer to another SharpSSH question Download file based on date modified from SFTP.


Though SharpSSH is a dead project, you should use another SFTP library:

  • SSH.NET has the method SftpClient.ListDirectory returning the IEnumerable<SftpFile>. The SftpFile has the .Length property;

  • WinSCP .NET assembly has the method Session.ListDirectory returning (via the RemoteDirectoryInfo.Files) a collection of the RemoteFileInfo with the property .Length.
    (I'm the author of WinSCP .NET assembly)