备份程序,分离文件路径复制到目标c#

本文关键字:复制 目标 路径 文件 程序 分离 备份 | 更新日期: 2023-09-27 18:11:08

我正在尝试做一个备份程序,这是我第一个帮助我学习的小项目之一。

到目前为止,我可以让它复制一个文件,但是当我添加一个文件列表时,它不复制。

它的工作方式是我点击添加文件,每个文件路径被添加到我的列表框,然后点击选择目的地,它将目的地添加到一个变量。

我正在使用代码从这里复制文件-复制一个目录的整个内容在c#

当我添加一个路径到列表框时,它会正常复制,但当我添加多个路径时,它基本上将我的两个路径字符串连接在一起而不工作。

是否有任何方法可以使它一个接一个地复制,或者我也可以将路径添加到数组中,然后从中读取它们?

//============================
//  GLOBALSCOPE VARIABLES
//============================ 
string backUpDes;
public Form1()
{
    InitializeComponent();
    backUpLbl.Visible = false;
    backUpDesLbl.Visible = false;
}
//============================
//         BUTTONS
//============================
//
//ABOUT BUTTON
//
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
    string message = "Adam" + "'n'r" + "Version 1" + "'n'r" + "Backup Program" + "'n'r" + "n3z";
    string caption = "Program Information";
    MessageBoxButtons buttons = MessageBoxButtons.OK;
    MessageBoxIcon icon = MessageBoxIcon.Information;
    MessageBox.Show(message, caption, buttons, icon);
}
//
//NEW BACKUP BUTTON
//
private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = addFilesDia.ShowDialog();
    if(result == DialogResult.OK)
    {
        listBox1.Items.Add(addFilesDia.SelectedPath);
    }
    else
    {
        if(MessageBox.Show("Please select a folder to backup", "Select a Folder", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
        {
            addFilesDia.ShowDialog();
        }
    }
}
//
//SELECT DESTINATION FOLDER BUTTON
//
private void button1_Click_1(object sender, EventArgs e)
{
    DialogResult result = desDia.ShowDialog();
    if (result == DialogResult.OK)
    {
        backUpLbl.Visible = true;
        backUpDesLbl.Visible = true;
        backUpDesLbl.Text = desDia.SelectedPath;
        backUpDes = desDia.SelectedPath;
    }
    else
    {
        if (MessageBox.Show("Please Select a Destination Folder for your backup.", "Select a Destination", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
        {
            desDia.ShowDialog();
        }
    }
}
//
//START BACKUP
//
private void startBtn_Click(object sender, EventArgs e)
{
    MessageBox.Show(listBox1.Items.Count.ToString());      //For testing stage only
    string path = "";
    foreach (var item in listBox1.Items)
    {
        path += item.ToString();
    }
    //Now Create all of the directories
    foreach (string dirPath in Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
        Directory.CreateDirectory(dirPath.Replace(path, backUpDes));
    //Copy all the files & Replaces any files with the same name
    foreach (string newPath in Directory.GetFiles(path, "*.*", SearchOption.AllDirectories))
        File.Copy(newPath, newPath.Replace(path, backUpDes), true);
    textBox2.Text = path;
}

备份程序,分离文件路径复制到目标c#

在事件startBtn_Click

中添加每个项目的路径字符串
    foreach (var item in listBox1.Items)
    {
        path += item.ToString();
    }

try with

    foreach (var item in listBox1.Items)
    {
        path = item.ToString();
        //Now Create all of the directories
        foreach (string dirPath in Directory.GetDirectories(path, "*",
           SearchOption.AllDirectories))
           Directory.CreateDirectory(dirPath.Replace(path, backUpDes));
        //Copy all the files & Replaces any files with the same name
        foreach (string newPath in Directory.GetFiles(path, "*.*",
           SearchOption.AllDirectories))
           File.Copy(newPath, newPath.Replace(path, backUpDes), true);
    }

据我所知,这是固定的。

 string path = "";
        for (int i = 0; i < listBox1.Items.Count; i++)
        {
            path = listBox1.Items[i].ToString();
            textBox2.Text += path;
            //Now Create all of the directories
            foreach (string dirPath in Directory.GetDirectories(path, "*",
               SearchOption.AllDirectories))
                Directory.CreateDirectory(dirPath.Replace(path, backUpDes));
            //Copy all the files & Replaces any files with the same name
            foreach (string newPath in Directory.GetFiles(path, "*.*",
               SearchOption.AllDirectories))
                File.Copy(newPath, newPath.Replace(path, backUpDes), true);
            //OLD CODE
            //foreach (var item in listBox1.Items)
            //{
            //    path = item.ToString();
            //    //Now Create all of the directories
            //    foreach (string dirPath in Directory.GetDirectories(path, "*",
            //       SearchOption.AllDirectories))
            //        Directory.CreateDirectory(dirPath.Replace(path, backUpDes));
            //    //Copy all the files & Replaces any files with the same name
            //    foreach (string newPath in Directory.GetFiles(path, "*.*",
            //       SearchOption.AllDirectories))
            //        File.Copy(newPath, newPath.Replace(path, backUpDes), true);
            //}
        }