即使在程序终止后,.NET 中的某些 File.IO 对象(如 DirectoryInfo)能否保留引用的可移动设备

发布于 2024-08-02 05:00:05 字数 876 浏览 6 评论 0原文

我在 .NET 中编写了一个程序,该程序递归遍历源目录和目标目录及其子目录的所有文件,比较最后写入时间,并根据比较结果将文件复制到目标目录或从目标目录删除文件。

当例如。目标目录是可移动驱动器 (USB) 上的目录,即使程序关闭后,我也无法从电脑上删除 USB 驱动器。没有其他程序打开 USB(例如资源管理器),并且该程序不再出现在任务列表中。

该程序的工作原理是使用 GetFiles 获取每个目录和子目录的 DirectoryInfo

DirectoryInfo dir = new DirectoryInfo(path);

来获取所有文件:

var files =dir.GetFiles();

然后 foreach 循环遍历所有文件以检查是否过滤掉某些文件(手动完成,因为我想要使用 RegEx 的多个模式)。

根据正则表达式过滤器未排除的文件将添加到 SortedList 中,一个用于源目录,一个用于目标目录。

这是由比较函数使用的。它使用 using 模式为源列表创建一个枚举器,为目标列表创建一个枚举器:

using (var srcEnum = _srcFileInfos.GetEnumerator())
{
    using (var dstEnum = _dstFileInfos.GetEnumerator())
    {
       ... // compare code
    }
}

最后,使用以下方式复制或删除文件

 File.Copy
 File.Delete

: 在内存管理方面是否存在我忽略的东西,即使在之后也会保留对 USB 驱动器的引用我关闭程序?

I have written a program in .NET that recurses through all files of a source and destination directory and its subdirectories, compares lastwritetime and copies/deletes files to/from the destination directory based upon comparison result.

When eg. the destination directory is a directory on a removable drive (usb), I can not remove the usb drive from my pc, even after the program is closed. There are no other programs that have the usb open (eg explorer) and the program does not appear in the task list anymore.

The program works by getting DirectoryInfo for each directory and subdirectories using

DirectoryInfo dir = new DirectoryInfo(path);

I use GetFiles to get all files:

var files =dir.GetFiles();

Then a foreach loops through all files to check for filtering out some files (done manually because I want multiple patterns using RegEx).

Files that are not excluded based on regex filters, are added to a SortedList, one for source dir, and one for destination dir.

This is used by the compare function. It creates an enumerator for the source list and one for the destination list using the using pattern:

using (var srcEnum = _srcFileInfos.GetEnumerator())
{
    using (var dstEnum = _dstFileInfos.GetEnumerator())
    {
       ... // compare code
    }
}

Finally, files are copy or deleted using

 File.Copy
 File.Delete

Is there something I'm overlooking in terms of memory management, that would keep references to the usb drive even after I close the program?

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

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

发布评论

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

评论(3

自演自醉 2024-08-09 05:00:05

您可能已关闭该程序,但请检查以确保该进程确实已终止。使用任务管理器找到该进程,然后将其关闭,然后检查任务管理器以确保它确实消失了。

You may have closed the program, but check to make sure that the process has really terminated. Use Task Manager to find the process, then close it, then check with Task Manager to make sure it's really gone.

我做我的改变 2024-08-09 05:00:05

您可以使用免费的 Unlocker 实用程序来枚举文件对象上存在哪些锁。或者,您可以使用 Sysinternals 的 HANDLE.EXE 列出所有句柄对于所有正在运行的进程。

You can use the free Unlocker utility to enumerate what locks exist on file object. Alternately, you can use Sysinternals' HANDLE.EXE to list all handles for all running processes.

向地狱狂奔 2024-08-09 05:00:05

我非常确定,从 USB 启动程序会导致 USB 设备锁定,尽管我不明白为什么。自从我开始为本地驱动器运行该程序以来,我再也没有遇到过锁定。也许是因为它是调试版本?不知道。

I pretty much sure, that starting the program from usb, caused the locking of the usb device, although I do not understand why. Since I started running the program for local drive, I experienced no locks anymore. Maybe because its the debug version? Don't know.

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