-2
это мой код.Исключение Обработка не обнаружена блок catch catch
void worker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
foreach (var a in fpath)
{
fin = new FileStream(a, FileMode.Open, FileAccess.Read, FileShare.Read);
total = fin.Length;
totalsize += total;
fin.Close();
}
foreach (var a in fpath)
{
dispatchertimer.Start();
stopwatch.Start();
filepath = System.IO.Path.GetFileName(a.ToString());
string destFile = System.IO.Path.Combine(DestinationPath, filepath);
fin = new FileStream(a.ToString(), FileMode.Open, FileAccess.Read, FileShare.Read);
filesize = fin.Length;
if (overwrite == true)
{
fout = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write);
while (fin.Position != filesize)
{
int n = fin.Read(buffer, 0, buffer.Length);
fout.Write(buffer, 0, n);
currentfilecopy += n;
PBvalue = currentfilecopy * 100/totalsize;
(sender as BackgroundWorker).ReportProgress((int)PBvalue);
}
fout.Flush();
fout.Close();
fileResultCollection.Add(new Result() { FileName = filepath, FileResult = "Pass" });
}
else
{
if (!File.Exists(System.IO.Path.Combine(DestinationPath, filepath)))
{
fout = new FileStream(destFile, FileMode.OpenOrCreate, FileAccess.Write);
while (fin.Position != filesize)
{
int n = fin.Read(buffer, 0, buffer.Length);
fout.Write(buffer, 0, n);
currentfilecopy += n;
PBvalue = currentfilecopy * 100/totalsize;
(sender as BackgroundWorker).ReportProgress((int)PBvalue);
}
fileResultCollection.Add(new Result() { FileName = filepath, FileResult = "Pass" });
}
else
{
//int n = fin.Read(buffer, 0, buffer.Length);
//fout.Write(buffer, 0, n);
filesize = fin.Length;
currentfilecopy += filesize;
PBvalue = currentfilecopy * 100/totalsize;
(sender as BackgroundWorker).ReportProgress((int)PBvalue);
fileResultCollection.Add(new Result() { FileName = filepath, FileResult = "Pass" });
}
}
}
}
#region CatchBlocks
catch (UnauthorizedAccessException ex)
{
fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail",FileName = filepath });
}
catch (ArgumentNullException ex)
{
fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath });
}
catch (ArgumentException ex)
{
fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath });
}
catch (PathTooLongException ex)
{
fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath });
}
catch (DirectoryNotFoundException ex)
{
fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath });
}
catch (FileNotFoundException ex)
{
fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath });
}
catch (IOException ex)
{
fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath });
}
catch (NotSupportedException ex)
{
fileResultCollection.Add(new Result() { FileReason = ex.Message, FileResult = "Fail", FileName = filepath });
}
#endregion
}
, что случилось с ним? – hurnhu
Какое исключение случается? Какая у вас ошибка? Может быть, исключение, которое происходит, отличается от того, что вы «ловите»? –
Добавить catch (Exception ex) { fileResultCollection.Add (новый результат() {FileReason = ex.Message, FileResult = "Fail", FileName = filepath}); } После всех исключений. –