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:
Hello,
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 :
C#
using WinSCP;

private void upload_file()
        {

            lblStatusMessage.Text = "";

            try
            {
                // Setup session options
                SessionOptions sessionOptions = new SessionOptions
                {

                    Protocol = Protocol.Sftp,
                    HostName = "abc.com",
                    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
                    session.Open(sessionOptions);

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

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

                    // Throw error
                    transferResult.Check();

                    // 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??
Posted
Updated 4-Aug-15 3:16am
v2
Comments
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
   
Good.
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 http://how2doinvbdotnet.blogspot.ae/2015/07/how-to-upload-and-download-files-in.html?view=flipcard
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