-2
これは私のコードです。例外処理が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(例外ex) { fileResultCollection.Add(新しいResult(){FileReason = ex.Message、FileResult = "Fail"、FileName = filepath}); }すべての例外の後。 –