Click here to Skip to main content
14,929,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to upload file in SFTP server and count number of file uploaded. For this first i add winSCPnet.dll . I am using below code :
using WinSCP;

private void upload_file()

            lblStatusMessage.Text = "";

                // Setup session options
                SessionOptions sessionOptions = new SessionOptions

                    Protocol = Protocol.Sftp,
                    HostName = "",
                    UserName = "XYZ",
                    Password = "%TGB5tgb",
                    PortNumber = 22,
                    SshHostKeyFingerprint = "ssh-rsa 1024 a7:20:03:28:19:3c:88:4c:05:3f:cf:c3:15:ad:57:20"


                using (Session session = new Session())
                    //sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey= true;
                    // Connect

                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    TransferOperationResult transferResult;
                    transferResult = session.PutFiles(@"P:\File_upload", "/Imaging/", false, transferOptions);

                    // Throw error

                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                        lblStatusMessage.Text = "Upload of " + transfer.FileName + " succeeded";


                //return 0;
            catch (Exception ex)
                lblStatusMessage.Text = "Exception: " + ex.Message;


** My code is working but now my problem is how to count number of file uploaded in server??
Updated 4-Aug-15 3:16am
Praveen Kumar Upadhyay 4-Aug-15 9:28am
You can count in your foreach loop. Set a counter variable above foreach loop and increase it by one in foreach loop.
Archana K 4-Aug-15 9:42am
yes :)
Praveen Kumar Upadhyay 4-Aug-15 9:53am
Archana K 4-Aug-15 10:08am
Now i want to know the name of sucessfully uploaded file and failed file name.So how i can do this.
sreeyush sudhakaran 5-Aug-15 4:29am
I have written a fine implementation of this in a class please find code here
Archana K 5-Aug-15 5:08am
Thank you :)
sreeyush sudhakaran 5-Aug-15 4:26am
create a list of class which contains status of upload say List<<statusupload>> where
Class StatusUpload{
string strfilename;
bool bUploadStatus;
int nRe-tryCount;

use this in your foreach

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900