0

I have been working on a small project in C # to upload XML files to an sftp server to a folder called "input". I was only able to do it with one file, and I need to upload all the files from a local repository, please help :(

var info = new SftpInfo()
        {

            Destination = @"./input/",
            Host = "subir.org",
            UserName = "admin1",
            Password = "1234",
            Port = 222
        };

        try
            {
                using (SftpClient client = new SftpClient(info.Host, info.Port, info.UserName, info.Password))
                {

                    client.Connect();
                    client.ChangeDirectory(info.Destination);
                    using (FileStream fs = new FileStream(@"D:/Archivos/archivo76781_P026977.xml", FileMode.Open))
                    {
                        client.BufferSize = 4 * 1024;
                        client.UploadFile(fs, Path.GetDirectoryName(@"D:/Archivos/archivo76781_P026977.xml"));
                        Console.WriteLine("successfully");
                        Console.WriteLine("*************************************");
                       
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An Error has Occured: " + ex);
                Console.WriteLine("Hay un error");
            }
  • 1. `Directory.GetFiles(path)` https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=net-5.0#System_IO_Directory_GetFiles_System_String_ | 2. A loop. – Christopher Aug 14 '21 at 18:37
  • In unrelated things, I noticed you are cathing exception. This is a deadly sin of exception handling. While you have to catch some exogenous excetpions, blindly catching exception is very bad. Please read these two: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ | https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET – Christopher Aug 14 '21 at 18:39
  • You can use a great nuget that is created for working with Ftp, it gives you fantastic abilities, like uploading and downloading a directory to or from the server. Have a look at this repo.https://github.com/robinrodricks/FluentFTP/blob/master/FluentFTP.CSharpExamples/UploadDirectory.cs – Mohammad Niazmand Aug 15 '21 at 03:37

0 Answers0