How to copy a file to another path?

后端 未结 9 1039
长发绾君心
长发绾君心 2020-11-27 04:59

I need to copy a file to another path, leaving the original where it is.

I also want to be able to rename the file.

Will FileInfo\'s CopyTo method work?

9条回答
  •  时光说笑
    2020-11-27 05:29

    TO Copy The Folder I Use Two Text Box To Know The Place Of Folder And Anther Text Box To Know What The Folder To Copy It And This Is The Code

    MessageBox.Show("The File is Create in The Place Of The Programe If you Don't Write The Place Of copy And You write Only Name Of Folder");// It Is To Help The User TO Know
              if (Fromtb.Text=="")
            {
                MessageBox.Show("Ples You Should Write All Text Box");
                Fromtb.Select();
                return;
            }
            else if (Nametb.Text == "")
            {
                MessageBox.Show("Ples You Should Write The Third Text Box");
                Nametb.Select();
                return;
            }
            else if (Totb.Text == "")
            {
                MessageBox.Show("Ples You Should Write The Second Text Box");
                Totb.Select();
                return;
            }
    
            string fileName = Nametb.Text;
            string sourcePath = @"" + Fromtb.Text;
            string targetPath = @"" + Totb.Text;
    
    
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);
    
    
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
                //when The User Write The New Folder It Will Create 
                MessageBox.Show("The File is Create in "+" "+Totb.Text);
            }
    
    
            System.IO.File.Copy(sourceFile, destFile, true);
    
    
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);
    
    
                foreach (string s in files)
                {
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
    
                }
                MessageBox.Show("The File is copy To " + Totb.Text);
    
            }
    

提交回复
热议问题