如何复制文件正在使用 [英] How to Copy a File that is In Use

查看:139
本文介绍了如何复制文件正在使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供我的用户提供发布日志文件到我的服务器,而不是要求他们能够找到它,并通过电子邮件发送给我的能力。

I want to provide my users with the ability to post a log file onto my server rather than requiring them to find it and email it to me.

我使用这个库 http://ftplib.codeplex.com/ 打开服务器上的ftp会话,并发送文件。有一点重命名参与,但就是这样。

I am using this library http://ftplib.codeplex.com/ to open an ftp session on the server and send the file. There is a bit of renaming involved but that is it.

不幸的是要发送的日志文件实际上是开放的,所以我得到了一个文件正由另一个进程使用例外。这是有道理的,当我认为它在迄今为止的日志我的应用程序运行时处于打开状态。我关闭了它,但是,当然,上载是一个漫长的过程。我把上传的代码放到一个后台线程,使用户可以继续。然而,日志不能被重新打开,直到上传完成。在此期间可能会有一些事件应写入日志。

Unfortunately the log file to be sent is actually open so I got a 'file is being used by another process' exception. This makes sense when I think about it in so far as the log is open while my app is running. I closed it but, of course, uploading is a long process. I put the upload code into a background thread so that the user may continue. However the log cannot be re-opened until the upload is complete. In the meantime there could be some event that should be written to the log.

所以我在寻找一种方式来复制日志,然后上传。什么是做到这一点的最好办法?该日志BTW二进制文件。

So I am looking for a way to copy the log and then upload it. What would be the best way to do that? The log is a binary file BTW.

推荐答案

如果你不拥有具有打开日志文件中的代码(即,它的另一个应用程序或一个封闭源代码的DLL),你可以尝试做一个 File.Copy(小于登录>中< tempdest>)和发送文件,删除它时,你完成了。这时候你没有读访问文件只是有时工作。

If you don't own the code that has the log file open (ie, it's another app or a closed source dll), you can try doing a File.Copy(<log>, <tempdest>) and send that file, deleting it when you're done. This only sometimes works when you don't have read access to the file.

如果你做自己正在访问的文件摆在首位的代码,你想有一个明确的ShareMode即

If you do own the code that is accessing the file in the first place, you want to open it with an explicit ShareMode ie

 File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)

这篇关于如何复制文件正在使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆