Header Ads

Folder copy

The utility contains two buttons to read the Source and Destination path.

private void button1_Click(object sender, EventArgs e)
{
string folderSelected = GetFolderPath();
if( !String.IsNullOrEmpty(folderSelected))
textBox1.Text = folderSelected;
}

private void button2_Click(object sender, EventArgs e)
{
string folderSelected = GetFolderPath();
if (!String.IsNullOrEmpty(folderSelected))
textBox2.Text = folderSelected;
}

private string GetFolderPath()
{
// Show the FolderBrowserDialog.
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();

DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
return folderBrowserDialog1.SelectedPath;
}

return string.Empty;
}


The above code opens the Folder browser dialog and requests the path.

The final button does the copy from the source to the destination folder.

private void button3_Click(object sender, EventArgs e)
{

if (!String.IsNullOrEmpty(textBox1.Text) && !String.IsNullOrEmpty(textBox2.Text))
{
if (Directory.Exists(textBox1.Text) )
{
Directory.Move(textBox1.Text, textBox2.Text);
MessageBox.Show("Folder moved!");
}
else
MessageBox.Show("Source or destination does not exist!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
MessageBox.Show("Source or destination is empty!","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

No comments:

Powered by Blogger.