ssh.net uploading with progress bar

Oct 11, 2014 at 3:58 PM
hi friends
how to use progress bar when uploading file ?
this is my code for uploading file to server

please help , thanks
private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
 
 
            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.Title = "Browse Text Files";
 
 
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
 
 
            openFileDialog1.DefaultExt = "txt";
            openFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
 
 
            openFileDialog1.ReadOnlyChecked = true;
            openFileDialog1.ShowReadOnly = true;
 
 
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = openFileDialog1.FileName;
            }
        }
 
 
        private void btnUpload_Click(object sender, EventArgs e)
        {
            using (Renci.SshNet.SftpClient sftp = new Renci.SshNet.SftpClient(txtHost.Text, txtUsername.Text, txtPassword.Text))
            {
                using (Stream fin = File.OpenRead(txtPath.Text))
                {
                    try
                    {
                        sftp.ConnectionInfo.Timeout = new TimeSpan(0, 0, 30);
 
 
                        sftp.Connect();
 
 
                        sftp.UploadFile(fin, "/upload/" + "salam.txt");
                        MessageBox.Show("Uploaded Succesfull");
                         
                    }
                    catch
                    {
                        MessageBox.Show("Uploading Error...");
                    }
                }
            }
        }
Oct 13, 2014 at 11:10 AM
please help meeee
Oct 14, 2014 at 11:34 AM
Thanks for contacting me. I hope you realise I am not an author of this library, though.
Your question is a bit off topic, progress bar is a UI control, nothing to do with Sftp.
Anyway, here's how you can get the upload progress, which you can then use with progress bar if you need to.

NOTE: Because of the way progress callback is currently implemented, your progress bar is likely to be jumping back and forth during upload.

You should be able to do something like this:
            using( Renci.SshNet.SftpClient sftp = new Renci.SshNet.SftpClient( "some.host", "some.file", "some.password" ) )
            {
                using( Stream fin = File.OpenRead( "file.in" ) )
                {
                    try
                    {
                        sftp.ConnectionInfo.Timeout = new TimeSpan( 0, 0, 30 );

                        sftp.Connect();

                        long total = fin.Length > 0 ? fin.Length : 1;
                        sftp.UploadFile(
                            fin,
                            "/upload/" + "salam.txt",
                            bytesUploaded =>
                            {
                                int percent = (int)((((double)bytesUploaded) / total) * 100.0 );
                                BeginInvoke( new Action( () => m_textBox1.Text = percent + "%" ) );
                            } );
                        MessageBox.Show( "Uploaded Successful" );

                    }
                    catch
                    {
                        MessageBox.Show( "Uploading Error..." );
                    }
                }
            }
You need to use BeginInvoke because progress callbacks will be called from threadpool threads. I assumed you are developing a Windows Forms application.

I hope this helps.

Sylwester
Marked as answer by drieseng on 10/14/2014 at 11:08 AM
Oct 14, 2014 at 2:56 PM
Thanks Sylwester. This was very helpful.
Coordinator
Oct 14, 2014 at 6:09 PM
Thanks on my behalf as well :p
Oct 15, 2014 at 2:28 PM
thanks sly_tm

but ...

Error 1 Cannot convert lambda expression to type 'bool' because it is not a delegate type C:\Users\abedi98\Documents\Visual Studio 2010\Projects\sftp\Security SFTP Application\Security SFTP Application\Form1.cs 218 25 Security SFTP Application