Get file size from SFTP using SharpSSH

会有一股神秘感。 提交于 2019-12-09 14:03:54

问题


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)



来源:https://stackoverflow.com/questions/26023293/get-file-size-from-sftp-using-sharpssh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!