Managing Files in C# - Copying, Moving, and Deleting (Page 3 of 5 )
Due to the nature of our project, we need to have a source and a destination file. That's what we are going to use on almost every action during our tutorial - copy, move, etc. The destination won't be use as frequently as the source, but we still need to have it prepared. To accomplish this, we are going to create two strings, sourceFileName and destFileName. After this, we can code the dialog boxes' [OK].
private void openFileDialog_FileOk(object sender, CancelEventArgs e)
{
sourceFileName = openFileDialog.FileName;
}
private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
{
destFileName = saveFileDialog.FileName;
}
You can guess what the above block of code does, right? It's intuitive. As soon as the user picks his/her source and destination files in the dialog boxes, those two variables will carry their address/path (i.e. C:Tempdatabase.txt).
Let the fun begin! Now we will code what happens when we click on btnCopy. The snippet is attached below, but first let me explain it in a nutshell. We display/run the dialog boxes because we need the source and destination paths. A quick bit of error handling is necessary because if there is a "null" or empty path, our copy method goes nuts. As soon as this is done we use System.IO.File.Copy() to copy the file. Then we notify the user with a pretty `Done with success` message box.
private void btnCopy_Click(object sender, EventArgs e)
{
sourceFileName = null;
destFileName = null;
openFileDialog.ShowDialog();
if (!System.IO.File.Exists(sourceFileName))
{
MessageBox.Show("The selected file either does not exist or you didn't pick any.");
return;
}
saveFileDialog.ShowDialog();
if (destFileName == null || destFileName == "")
{
MessageBox.Show("You didn't pick the destination path.");
return;
}
System.IO.File.Copy(sourceFileName, destFileName);
MessageBox.Show("Done with success.");
}
We do the same for btnMove, too. The only difference is that we will use the System.IO.File.Move() to accomplish the file-moving action. The rest remains the same.
private void btnMove_Click(object sender, EventArgs e)
{
sourceFileName = null;
destFileName = null;
openFileDialog.ShowDialog();
if (!System.IO.File.Exists(sourceFileName))
{
MessageBox.Show("The selected file either does not exist or you didn't pick any.");
return;
}
saveFileDialog.ShowDialog();
if (destFileName == null || destFileName == "")
{
MessageBox.Show("You didn't pick the destination path.");
return;
}
System.IO.File.Move(sourceFileName, destFileName);
MessageBox.Show("Done with success.");
}
Next is btnDelete, because it's the easiest. We will skip renaming and grabbing the file attributes for now. You will find those on the next page. Delete works based on a sourceFileName, so requiring the user to specify a destination file is silly! We will do the usual error handling and then, for convenience, we'll display a confirmation message box. The user might miss-click or your code might be buggy and then something important becomes deleted. After clicking "Yes," we delete the file.
private void btnDelete_Click(object sender, EventArgs e)
{
sourceFileName = null;
openFileDialog.FileName = null;
openFileDialog.ShowDialog();
if (!System.IO.File.Exists(sourceFileName))
{
MessageBox.Show("The selected file either does not exist or you didn't pick any.");
return;
}
if (MessageBox.Show("Are you sure? You are about to delete the following file: " + sourceFileName, "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
System.IO.File.Delete(sourceFileName);
MessageBox.Show("Done with success.");
}
}
The rename and properties parts are a bit longer, but that doesn't necessarily mean they're more complex or advanced. We'll do our best to explain what each snippet does and why we opted for that type of algorithm, have no fear. Turn the page, if you're ready.
Next: Renaming and File Attributes >>
More C# Articles
More By Barzan "Tony" Antal