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

Advertisement

Aditya
Guest

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

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);
}

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
27,226
Location:
Prague, Czechia

Re: Winscp RemoteFileInfo how to convert the file into byte array in C# .net

My response to your duplicate post on Stack Overflow:
https://stackoverflow.com/q/35312694/850848

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.

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);
}    

Reply with quote

Aditya
Guest

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

Can you more elaborate like how you get the path and all using remote file info

Reply with quote

Advertisement

You can post new topics in this forum