Copy All files from one folder to other folder in Asp.net

Hi ,
Here am creating th edirectory at runtime and am coping the All files from one folder to other folder  at a time.

<div>
    <asp:textbox ID="Textbox1" runat="server"></asp:textbox><br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <br />
         <asp:Label ID="lblMessage" runat="server"/>
    </div>

C# code
 protected void Button1_Click(object sender, EventArgs e)
    {
        string directoryPath = Server.MapPath("~/") + Textbox1.Text;
        //string[] f = Directory.GetFiles(Server.MapPath("/Account"));
      if (!Directory.Exists(directoryPath))
      {
          Directory.CreateDirectory(directoryPath);
          lblMessage.Text = "Directory created";
          DirectoryInfo dir1 = new DirectoryInfo(Server.MapPath("~/Account"));
          DirectoryInfo dir2 = new DirectoryInfo(Server.MapPath("~/") + Textbox1.Text);
          foreach (FileInfo fi in dir1.GetFiles())
          {
              fi.CopyTo(Path.Combine(dir2.ToString(), fi.Name), true);
          }
 }
      else
          lblMessage.Text = "Directory already exists";
    
    }

or
 protected void Button1_Click(object sender, EventArgs e)
    {
        string directoryPath = Server.MapPath("~/") + Textbox1.Text;
        //string[] f = Directory.GetFiles(Server.MapPath("/Account"));
      if (!Directory.Exists(directoryPath))
      {
          Directory.CreateDirectory(directoryPath);
          lblMessage.Text = "Directory created";
           string[] filePaths = System.IO.Directory.GetFiles(@"D:\sandeep\Account");
      foreach (String fileName in filePaths)
      {
          string targetFolder = Server.MapPath("~/") + Textbox1.Text;
          System.IO.FileInfo fi = new System.IO.FileInfo(fileName);
          fi.CopyTo(System.IO.Path.Combine(targetFolder, fi.Name), true);
      }
 }
      else
          lblMessage.Text = "Directory already exists";
    
    }

Comments

Popular posts from this blog

what is Event Cache table in sharepoint

CAML Query syntax and options in SharePoint

Change anchor link url in sharepoint calender