Sunday 20 May 2018

Copy Files From source to Destination by renaming from table


Add System.Coniguration dll from the references and then proceed with the below code


Button Click Event Code:

 var mrmConnectionString = ConfigurationManager.ConnectionStrings["MRMConnection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(mrmConnectionString))
            {
                var splitFileNames = "\'"+textBox1.Text.Replace("\r\n","\',\'")+"\'";

                SqlDataAdapter da = new SqlDataAdapter("select * from sample where filename in (" + splitFileNames + ") ", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Columns.Count > 1)
                {
                    //var src = @"C:\Users\Admin\Desktop\Src\";
                    var src = ConfigurationManager.AppSettings["SourceLocation"].ToString();

                    //var dest = @"C:\Users\Admin\Desktop\Dest\";
                    var dest = ConfigurationManager.AppSettings["DestinationLocation"].ToString();
                 
                    foreach (DataRow row in dt.Rows)
                    {
                        string name = row["FileName"].ToString();
                        string fileName = row["ImageFileName"].ToString();
                        File.Copy(src + name, Path.Combine(dest, Path.GetFileName(fileName)), true);
                    }
                }


            }

ConnectionString add-ons:

<appSettings>
    <add key="SourceLocation" value="C:\Users\Admin\Desktop\Src\" />
    <add key="DestinationLocation" value="C:\Users\Admin\Desktop\Dest\" />
  </appSettings>
  <connectionStrings>
    <add name="MRMConnection" connectionString="Data Source=ADMIN-PC\SA;Initial Catalog=mydb;Integrated Security=True"/>

  </connectionStrings>

No comments:

Post a Comment