Topic "Winscp RemoteFileInfo how to convert the file into byte array in C# .net"

Author Message
Aditya

Guest


I am in trouble over on issue,I have to read a file content or read the file bytes and need to store in it our database.

How to do it in WinScp in .net System.Io.File we can simply call File.ReadAllBytes(file).

How to do it in Winscp RemoteFileInfo.

Sample code .

using (Session session = new Session())
{
session.Open(sessionOptions);
if (objFTP.RequestFor == "FR")
{
//sDirectoryPath.Append(objFTP.ReferenceNo);
directory = session.ListDirectory(basePath+objHCSAuthenticate.Source+"/"+objFTP.ReferenceNo); // Creation winscp Directory
}
else
{
//sDirectoryPath.Append(objFTP.PreAuthNo);
directory = session.ListDirectory(basePath + objHCSAuthenticate.Source + "/" + objFTP.PreAuthNo); // Creation winscp Directory
}



}
foreach(RemoteFileInfo fileInfo in directory.Files)
{

string sFileNAme = string.Empty;
string sDocShortCode = string.Empty;
string sDocType = string.Empty;

// sFileNAme = Path.GetFileName(file1);
sFileNAme = fileInfo.Name;
string[] FileNameArray = sFileNAme.ToString().Split('_');
sDocShortCode = FileNameArray[1]; //Get DocumentShortCode
sDocType = GetDocumentType(sDocShortCode);
//byte[] bytes = File.ReadAllBytes(file1);

byte[] bytes =System.Text.Encoding.UTF8.GetBytes(fileInfo.GetHashCode);
}
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
My response to your duplicate post on Stack Overflow:
http://stackoverflow.com/q/35312694/850848

Quote:
WinSCP .NET assembly does not support "downloading" remote file contents to memory.

All you can do is to download the file to a local temporary location and read it to the memory from there.

Code:
using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);
     
    // Download to a temporary folder
    string localPath = Path.GetTempFileName();
    session.GetFiles(remotePath, localPath).Check();
   
    // Read the file contents
    byte[] contents = File.ReadAllBytes(localPath);
   
    // Delete the temporary file
    File.Delete(localPath);
}
Aditya

Guest


Can you more elaborate like how you get the path and all using remote file info
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