This project is read-only.

Setting transfer mode to binary

Apr 23, 2014 at 3:18 PM
I am copying .wav phone recordings from a linux server to a windows share but the files are corrupted and unplayable after the copy is complete. I'm thinking this is because they are not being transferred as binary and I am unable to find a setting for this. The code is pretty simple:

ConnectionInfo info = new PasswordConnectionInfo(host, user, pwd);
        List<SftpFile> lst;
        using(SftpClient objSftp = new SftpClient(info))
        {
            objSftp.Connect();              
            lst = objSftp.ListDirectory(strSrc).ToList();
            foreach (SftpFile file in lst)
            {
                if (!file.IsDirectory)
                {
                    if (file.Name == "Test_Record.wav")
                    {
                        MemoryStream ms = new MemoryStream();
                        objSftp.DownloadFile(file.FullName, ms);

                        using (FileStream fs = new FileStream(strTarget + "\\" + file.Name, FileMode.Create, System.IO.FileAccess.Write))
                        {
                            byte[] bytes = new byte[ms.Length];
                            ms.Read(bytes, 0, (int)ms.Length);
                            fs.Write(bytes, 0, bytes.Length);
                            ms.Close();
                        }
                    }
                }
            }

            objSftp.Disconnect();
        }

Any ideas?
Apr 23, 2014 at 4:06 PM
I actually found the error here. I needed to set the memoryStream position to 0.

ms.Postion = 0;

This fixed the problem.
Marked as answer by erikgmiller on 4/23/2014 at 8:06 AM