N
needin4mation
I know there is no On Error or the like in C#. But I am not sure how
to make this code work when the file it tries to copy is in use, not
there, etc. Here is what I have tried and when the exception is thrown
the listbox updates, but the program just dies. I want it to go to the
next file in my list. Thank you for any help:
private void btnCopyFiles_Click(object sender, System.EventArgs e)
{
try
{
foreach (ListViewItem item in listViewFiles.Items)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = 4; //filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file
being copied.
pBar1.Step = 1;
string source=item.SubItems[0].Text.Trim();
string destination=item.SubItems[1].Text.Trim();
lisInfoList.Items.Add("Copying: " + source + " to " + destination);
File.Copy(source,destination,true);
item.SubItems[2].Text="Success";
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
Application.DoEvents();
}
}
catch (Exception exc)
{
lisInfoList.Items.Add("Error Copying: " + exc.Message + ", " +
exc.Source);
return;
}
to make this code work when the file it tries to copy is in use, not
there, etc. Here is what I have tried and when the exception is thrown
the listbox updates, but the program just dies. I want it to go to the
next file in my list. Thank you for any help:
private void btnCopyFiles_Click(object sender, System.EventArgs e)
{
try
{
foreach (ListViewItem item in listViewFiles.Items)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = 4; //filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file
being copied.
pBar1.Step = 1;
string source=item.SubItems[0].Text.Trim();
string destination=item.SubItems[1].Text.Trim();
lisInfoList.Items.Add("Copying: " + source + " to " + destination);
File.Copy(source,destination,true);
item.SubItems[2].Text="Success";
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
Application.DoEvents();
}
}
catch (Exception exc)
{
lisInfoList.Items.Add("Error Copying: " + exc.Message + ", " +
exc.Source);
return;
}