复制文件的最安全方法

发布于 2024-08-26 19:27:02 字数 1250 浏览 6 评论 0原文

我需要合并两个 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);
                        }
                    }

I need to merg two PDF files.
However sometimes a file might be locked up

I wrote this code, but I'm wondering if it's not the smartest solution:

     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);
                        }
                    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方觉久 2024-09-02 19:27:02

您应该添加一个计时器,以便在再次尝试文件操作之前先休眠几次。

另外你应该有计数器,这样你就不会无限期地等待,并且在 15 次尝试后退出。

You should add a timer so that you sleep for a few clicks before you try the file operation again.

Also you should have counter so you do not wait indefinitely and that you exit after say 15 tries.

[旋木] 2024-09-02 19:27:02

嗯,这取决于:
1)它是否是一个独立于用户的系统内部进程?如果是这样,您应该尝试找出锁定文件的内容并显式等待。随机等待然后一遍又一遍地尝试可能会导致问题。

2) 是否是可以打开该文件的用户?在这种情况下,等待是没有帮助的,因为系统可能会因为用户突然离开而在整个周末重试。您无法控制用户时间。只需告诉用户您无法执行请求的操作,因为文件已打开,然后让他们重试。

通常等待N秒/分钟并不是真正的解决方案。要么你知道问题可能是什么,然后进行民意调查和调查。解决问题,否则您实际上无能为力,只能发出通知。

Well this depends:
1) Is it a process that is all internal to a system independent of a user? If so you should try to find out what is locking the file and wait for the explicitly. Waiting randomly and then trying over and over again may cause problems on its own.

2) Is it a user that may have the file open? In this case waiting is not helpful since the system could retry all weekend because the user suddenly left for the day. You have no control over user timing. Just tell the user that you cannot do the requested operation because the file is open and have them try again.

Usually waiting for N seconds/minutes is not really a solution. Either you know what the problem may be and poll & resolve the issue or you can't really do anything and just send out notice.

无人接听 2024-09-02 19:27:02

没有特殊的函数可以做到这一点。实际上,即使这个功能存在,某些进程仍然可以轻松地在“锁定检查”和“文件打开”之间锁定该文件

There is no special function to do this. Actually even if this function exists, some process can still easily lock this file between your "lock check" and "file open"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文