Download file via FTP with current date C#

Shaggy1
Shaggy1 used Ask the Experts™
on
I need to download a file from our FTP server that has the current date. Each day the file changes.
Below is the code i have so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Checksums;
 
 
 
 
 
namespace FileDownload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        string filePath;
        string fileName;
        string ftpServerIP;
        string ftpUserID;
        string ftpPassword;
 
 
 
        private void button1_Click(object sender, EventArgs e)
        {
 
            FtpWebRequest reqFTP;
 
 
            try
            {
                filePath = "c:/Documents and Settings/user/foldername";
                fileName = "file.csv.gz";  
                ftpServerIP = "ftp.myipishere.com";
                ftpUserID = "myuserID";
                ftpPassword = "mypassword";
 
 
                FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
 
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new
 Uri("ftp://" + ftpServerIP + "/" + fileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
 
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
 
                ftpStream.Close();
                outputStream.Close();
                response.Close();
 
                progressBar1.Maximum = 3;
                progressBar1.Minimum = 0;
                progressBar1.Value = 0;
                progressBar1.Step = 1;
                progressBar1.PerformStep();
 
                rtbMain.Text = "Downloaded " + fileName + " to " + filePath + " !";
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
          }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I haven't used that library before. What exactly is the problem? Are you just looking for a way to produce a date-based filename in a string?

Author

Commented:
I am trying to download a file from a FTP server that has the current date only. There are multiple files on the server. Each day a new file is added. So i need to download only that file (current date) and move that to a local folder. In the code above, i can name a specific file and successfully download it. Hope I make sense.
Ok, so all you need is a string variable, containing the filename, using the date, right? You already have the rest of the solution? You haven't mentioned what the format is, so I'll just get you started. You can take it from there, to get to the specific format.

string dateString = DateTime.Now("MMddyyyy");
string filename = string.Format("file{0}.csv.gz", dateString);
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

Commented:
Ok, I declared the string dateString and fileName with the others as follows:
string filePath;
string ftpServerIP;
string ftpUserID;
string ftpPassword;
string fileName;
string dateString;

now, when i try to add the  dateString = DateTime.Now("MM/dd/yyyy"); to the "try" block (above code) im getting an error saying that it is a 'property' but acting as  method. Am i structuring this right?  Thanks for the help.
oops. Sorry. Use this:

DateTime.Now.ToString("pattern")

Author

Commented:
Thanks, works great. Appreciate it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial