SFTPClient.UploadFile - File not found

May 29, 2012 at 3:40 PM

Hi,

I can use FTP protocol as perfectly.

But when i try the following code then i getting error(File not found).

Have a good day.


SftpClient client = new SftpClient(host, port, userName, pass);
client.Connect();
if (client.IsConnected)
{
        Stream stream = File.OpenRead(dosyaYolu);
         client.UploadFile(stream, hedefDosyaYolu, true);
}
client.Disconnect();         
     

Jun 3, 2012 at 10:04 PM

    class Program
    {
        static void Main(string[] args)
        {
            SftpClient client = new SftpClient(@"network.site.com", 22, @"username", @"password");
            client.Connect();
            if (client.IsConnected)
            {
                //Console.WriteLine("Hello World!!!!");
                string FILE_NAME;
                FILE_NAME = @"C:\Temp\Test.txt";
                if (!File.Exists(FILE_NAME))
                {
                    Console.WriteLine(@"{0} does not exist on local.", FILE_NAME);
                    return;
                }

                Stream stream = new MemoryStream(ReadFile(FILE_NAME));
                client.UploadFile(stream, @"/kaplan/Test.txt", true);
                stream.Close();
                stream.Dispose();
            }
            client.Disconnect();
        }
        public static byte[] ReadFile(string filePath)
        {
            byte[] buffer;
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            try
            {
                int length = (int)fileStream.Length;  // get file length
                buffer = new byte[length];            // create buffer
                int count;                            // actual number of bytes read
                int sum = 0;                          // total number of bytes read

                // read until Read method returns 0 (end of the stream has been reached)
                while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                    sum += count;  // sum is a buffer offset for next reading
            }
            finally
            {
                fileStream.Close();
            }
            return buffer;
        }

    }

Jun 3, 2012 at 10:23 PM

Sorry,

I used your code, also and it worked.  I just had to put a / in front of the path in the uploadfile function.

SftpClient client = new SftpClient(host, port, userName, pass);
client.Connect();
if (client.IsConnected)
{
        Stream stream = File.OpenRead(@"C:\dosyaYolu");
         client.UploadFile(stream,@"/hedefDosyaYolu", true);
}
client.Disconnect();     

Jun 4, 2012 at 6:57 AM

Thank you elarson006.

I will try again. I think the problem about local or remote system security.

Have a good day..