我需要合并两个PDF文件。 但是有时文件可能被锁定复制文件的最安全方法
我写了这个代码,但我想知道,如果它不是最聪明的解决方案:
private static int FILE_LOCKED_WAIT_PERIOD = 1000;
while (true)
{
// If the file is in use, IOException will be thrown.
// If file is not permitted to be opened because of Permission
// Restrictions, UnauthorizedAccessException will be thrown.
// For all other, Use normal Exception.
try
{
inputDocument1 = PdfReader.Open(fileToMerge, PdfDocumentOpenMode.Import);
break;
}
catch (IOException)
{
Thread.Sleep(FILE_LOCKED_WAIT_PERIOD);
}
catch (UnauthorizedAccessException)
{
Thread.Sleep(FILE_LOCKED_WAIT_PERIOD);
}
catch (Exception)
{
Thread.Sleep(FILE_LOCKED_WAIT_PERIOD);
}
}
可能的重复:http://stackoverflow.com/questions/1563191/c-cleanest-way- to-write-retry-logic – 2010-03-30 21:24:32
@aron:让我们知道上面的链接是否回答您的问题。它看起来不像你的问题的确切副本,但内容可能仍然有帮助。 – 2010-03-30 21:29:08