0

I've written this to check whether the filename in a table column exists on ftp server directory if it exists then update (using update query) the column Isexists with value 1 else keep as it is.

I'm using DataTable to read each row (filename) then making FtpWebRequest all time to check whether the file exists of not.

This way CheckIfFtpFileExists(string fileUri) getting call for the numbers of times is equal to the no. of rows in the DataTable.

Is there any way I can make FTP auth. only once and then call the function.

Any suggestion How can I improve my code to increase the speed of.

namespace FTP_File_Check
{
    class Program
    {
        static void Main(string[] args)
        {
            string connString = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DMSTG;Data Source=.";



   string sql = @"SELECT full_file_name from Intellitrack.S_Sr_att_Delta_Consolidated_28082012 where Isexists=0";

        SqlConnection conn = new SqlConnection(connString);
           conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);


            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn col in dt.Columns)
                {


                    SqlConnection sqlConnection1 = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DMSTG;Data Source=.");
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandType = CommandType.Text;

                    if (CheckIfFtpFileExists("ftp://myftpaddressgoeshere.com/0708/" + row[col].ToString()))
                    {
                      // Found File Exists
                        cmd.CommandText = @"UPDATE Intellitrack.S_Sr_att_Delta_Consolidated_28082012 
                                       SET Isexists = 1 
                                         where full_file_name='" + row[col].ToString() + "'";
                        cmd.Connection = sqlConnection1;

                        sqlConnection1.Open();

                       cmd.ExecuteNonQuery();
                        sqlConnection1.Close();

                    }


                }

            }

    }

    public static bool CheckIfFtpFileExists(string fileUri)
    {

            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(fileUri);
            request.Credentials = new NetworkCredential("myuserid", "mypassword");
            request.KeepAlive = true;
            request.ConnectionGroupName = "file";
            request.ServicePoint.ConnectionLimit = 8;
            request.UseBinary = true;
            request.UsePassive = false;

            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;


            try
            {

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            }
            catch (WebException ex)
            {
                FtpWebResponse response = (FtpWebResponse)ex.Response;
                if (FtpStatusCode.NotLoggedIn == response.StatusCode)
                {
                    Console.WriteLine("Failed to connect to ftp server");
                    Console.ReadLine();
                    Environment.Exit(99);

                }
                else
                {

                    if (FtpStatusCode.ActionNotTakenFileUnavailable == response.StatusCode)
                    {

                        return false;
                    }
                }
            }

        return true;
        }


    }



}
2
  • are the files all in the same remote directory?
    – paul
    Aug 28, 2012 at 11:01
  • @paul yes on remote server in one directory. Aug 28, 2012 at 11:02

1 Answer 1

-1

perhaps you could use WebRequestMethods.Ftp.ListDirectoryDetails, this would give you a list of all the remote files with one call. You could store this in a List<string> and then just check each row of the database against this list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.