在c#中使用sftp解压缩文件夹上传和下载

时间:2018-03-23 06:37:24

标签: c# upload directory sftp unzip

我可以使用下面的代码成功上传文件或压缩文件夹,但我想上传和下载解压缩文件夹。当我尝试在代码下面上传文件夹uisng它给出错误 请帮忙解决这个问题 文件上传代码成功运行: -

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("-------------------------------------------------------------------------------");
                }
                }

0 个答案:

没有答案