I am trying to make a backup program, one of my first small projects to help me learn.
So far, I can get it to copy 1 file, but when I add a list of files, it does not copy.
The way it works is i click add files, each file path is added to my listbox, then click Select Destination, it adds the destination to a variable.
I am using code from here to copy files - Copy the entire contents of a directory in C#
It will copy ok when I add 1 path to the listbox, but when I add more than one, it basically joins my 2 path strings together and doesn't work.
Is there any way to make it copy 1 by one, or would I have too add the paths to an array, and then read them from it?
//============================
// 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;
}