Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

C# FTP Send Multiple Files, log in only once

$
0
0
I have an FTP account on drivehq.com, and I'm trying to send all my PDF files from my desktop to it. But, because at each iteration ( i'm iterating through a string array, which contains the path file of the all PDF files from my desktop ) I'm creating a new FtpWebRequest, drivehq has a built-in antispam upload file, and they block my access for a while. Can somebody make this routine / script to use the FtpWebRequest only once, and to upload all PDF files?

   
 // The PDFiles array ( desktopDir = My Desktop Directory )

        List<string> PDFiles = Directory.GetFiles(desktopDir, "*.pdf", SearchOption.AllDirectories).Where(x => File.GetLastAccessTime(x).Date.ToShortDateString() == DateTime.Today.Date.ToShortDateString())
                                                  .Select(x => x)
                                                  .ToList();
 // Upload the files
        FtpWebRequest requestFTPUploader;
        FileInfo fileInfo;
        FileStream fileStream;
        Stream uploadStream;
        int bufferLength = 2048;
        int contentLength;

        for (int i = 0; i < PDFiles.Count; ++i)
        {            
                requestFTPUploader = (FtpWebRequest)WebRequest.Create("ftp://ftp.drivehq.com/" + Path.GetFileName(PDFiles[i]));
                requestFTPUploader.Credentials = new NetworkCredential("MyAccount", "MyPassword");
                requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;

                fileInfo = new FileInfo(PDFiles[i]);
                fileStream = fileInfo.OpenRead();

                byte[] buffer = new byte[bufferLength];

                uploadStream = requestFTPUploader.GetRequestStream();
                contentLength = fileStream.Read(buffer, 0, bufferLength);

                while (contentLength != 0)
                {
                        uploadStream.Write(buffer, 0, contentLength);
                        contentLength = fileStream.Read(buffer, 0, bufferLength);
                }

                uploadStream.Close();
                fileStream.Close();

                requestFTPUploader = null;
        }              




    

Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>