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

时间:2015-09-04 14:17:18

标签: c# file io backup copy-paste

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

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

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

我使用此处的代码复制文件 - Copy the entire contents of a directory in C#

当我向列表框添加1个路径时它会复制好,但是当我添加多个路径时,它基本上将我的2个路径字符串连接在一起并且不起作用。

有没有办法让它逐个复制,或者我是否也将路径添加到数组中,然后从中读取它们?

//============================
//  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;
}

2 个答案:

答案 0 :(得分:0)

您要在Event startBtn_Click

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

尝试

    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);

    }

答案 1 :(得分:0)

据我所知,这是固定的。

 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);
            //}
        }