0

I can successfully upload file or zipped folder using below code but i want to upload and download unzip folder. when I try to upload folder uisng below code it giving error Please help to solve this problem
File upload code working successfully:-

string destination = null;
                string host = "";
                string username = "";
                string password = "";
                string[] imagefiles = null;
                string imageFileName = null;
                string imageReadPath = ConfigurationManager.AppSettings["ImageReadPath"];
                int port = ; 

                if (Directory.Exists(imageReadPath)) imagefiles = Directory.GetDirectories(imageReadPath);

    foreach (string imagePath in imagefiles)
                {
    try
                {
                    Console.WriteLine("-------------------------------------------------------------------------------");
                    Console.WriteLine("Host    :- " + host);
                    Console.WriteLine("Username:- " + username);
                    Console.WriteLine("Port    :- " + port);
                    Console.WriteLine("SFTP Connection Start");

                    using (SftpClient client = new SftpClient(host, port, username, password))
                    {
                        client.Connect();
                        if (client.IsConnected)
                        {
                            Console.WriteLine("SFTP Connect");
                            client.ChangeDirectory(@"/cygdrive/g/New folder/");

                            DirectoryInfo d = new DirectoryInfo(sourcefile);



                            using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
                            {
                                client.BufferSize = 4 * 1024;

                                client.UploadFile(fs, Path.GetFileName(sourcefile), null);//upload file using sftp code 
                                Console.WriteLine("File " + sourcefile + " upload successfully");
                            }
                        }
                    }

                    Console.WriteLine("SFTP Connection Close");
                    Console.WriteLine("-------------------------------------------------------------------------------");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error   :-" + ex.Message.ToString());
                    Console.WriteLine("-------------------------------------------------------------------------------");
                }
                }
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • list your error.... – BugFinder Mar 23 '18 at 07:11
  • What is the value of `sourcefile`? It is file name? The why do you use `DirectoryInfo` on file name? *(furthermore the `d` is never used)* When it is directory, why do you use it as input for file stream? When you want directory upload get files from directory and use `foreach` to upload each file. – Julo Mar 23 '18 at 07:12
  • See [SSH.NET Upload whole folder](https://stackoverflow.com/q/39397746/850848). – Martin Prikryl Mar 23 '18 at 07:45
  • I forgot to comment directoryinfo d please this line –  Mar 23 '18 at 08:41
  • You comment makes no sense. Did the answer in the linked question help? – Martin Prikryl Mar 23 '18 at 12:38

0 Answers0