调试 c#/.NET 中的文件句柄问题

发布于 2024-12-27 23:09:38 字数 555 浏览 3 评论 0原文

我有一个程序反过来访问 DLL。它以多种方式使用文件,创建文件、复制文件并将它们作为 emailMessage 对象的附件。我遇到“文件正在使用”错误,阻止覆盖文件。

是否有任何工具或技术可以简化该过程 - 例如,创建/删除包含特定字符串的句柄时发出某种警告?我看过进程资源管理器、进程监视器(filemon 的后继者)等,但没有运气。

我可以继续寻找,但认为可能有比继续通过代码进行非正式调查更好的方法。

编辑

感谢您的评论。就问题而言,结构有点复杂。我有我的“客户端”应用程序,它引用“引擎”dll(也在 C# 中)。两者都是我自己的。客户端创建“附件”实例,每个附件都包含对各种文件的文件引用。它的构造函数获取引用的文件,并将副本放入临时文件夹中。随后,一组“操作”构建电子邮件 - 它将每个附件作为“真实”附件添加到 EmailMessage 对象中。我多次处理这些对象并将它们的引用设置为空,但如果我太早这样做,那么最终会出现空引用异常。如果我做得太晚了,那该死的东西就被使用了!我确信我可以在适当的时候找到这个特定的错误,但它促使我想知道是否有一个有用的标准方法可供知情者使用。 :)

I have a program that in turn accesses a DLL. It uses files in various ways, creating them, copying them and establishing them as attachments to an emailMessage object. I have a 'file in use' error, preventing overwrite of a file.

Is there any tool or technique for easing the process - eg some kind of warning of the creation / removal of handles containing a particular string? I've looked at process explorer, process monitor (the successor to filemon) etc but no luck.

I can keep hunting, but thought there might be a better approach than continuing my informal investigations through the code.

EDIT

Thanks for the comments. Regarding the questions, the structure is a bit complex. I have my 'client' app which references an 'Engine' dll (in C# also). Both are my own. The client creates instances of 'attachments' each of which contain a file reference to various files. The constructor of that grabs the file referred to, and puts a copy in a temporary folder. Later, a set of 'actions' builds the email - it adds each attachment as a 'real' attachment to an EmailMessage object. At various times I dispose of the objects and set their references to null, but if I do it too early then I end up with null reference exceptions. If I do it too late, the damn things are in use! I'm sure I can hunt down this specific bug in due course, but it prompted me to wonder if there is a useful standard method used by those in the know. :)

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

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

发布评论

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

评论(2

魔法少女 2025-01-03 23:09:38

Procmon 会告诉您谁(即哪个进程)正在打开该文件,以及打开该文件的位置的(非托管)堆栈。

C#/.NET 中此问题的常见原因是人们打开文件流,但在使用完毕后没有将其释放。这意味着文件将保持打开状态,直到(至少)下一次垃圾回收,这可能会持续很长时间。

Procmon will tell you who (i.e. which process) is opening the file, and the (unmanaged) stack of the place that opened it.

The usual cause of this problem in C#/.NET is people opening file streams and not disposing when they've finished with them. This means the file stays open until (at least) the next garbage collection, which can be ages away.

羁〃客ぐ 2025-01-03 23:09:38

听起来您正在寻找一个工具来让您知道调试时是什么锁定了文件。如果是这样,您可以使用 Unlocker 来确定锁定文件的进程。

It sounds like you're looking for a tool to let you know what is locking the file while debugging. If so, you can use Unlocker to determine the process locking the file.

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