FileInfo 锁定文件!

发布于 2024-09-15 19:02:55 字数 614 浏览 1 评论 0原文

想象一个 winform 应用程序,它将更新的程序集从源文件夹 A 复制到目标文件夹 B。 我使用简单的 DirectoryInfo.GetFiles 方法来填充列表视图,比较文件夹 A 和 B 中程序集的版本;如果某些程序集较新,我将启动更新方法。 在这个方法中,在复制之前,我尝试一下B文件夹中的所有文件是否都没有被使用:

var B = new DirectoryInfo("myBfolder");
foreach (var file in aFolder.GetFiles())
{
    try
    {
        //File not in use
        File.Open(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (Exception ex)
    {
        //File in use!
    }
}

嗯,因为之前的UpdateListView代码,使用FileInfo来获取要显示的信息,所以我的所有文件都被使用了!

FileInfo 锁定文件!这可能吗?

有人可以建议一种方法来绕过这个问题吗?

谢谢你, 南多

Imagine a winform app, who copy updated assemblies from a source folder A to a destination folder B.
I use simple DirectoryInfo.GetFiles methods to fill a listview, comparing version of assembly in folder A and B; if some assemblies are newer, I start my update method.
In this method, before copying, I try if all file in B folder are not in use:

var B = new DirectoryInfo("myBfolder");
foreach (var file in aFolder.GetFiles())
{
    try
    {
        //File not in use
        File.Open(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
    }
    catch (Exception ex)
    {
        //File in use!
    }
}

Well, because of previous UpdateListView code, that use FileInfo to get info to show, all my files results in use!

FileInfo lock files! Is this possible?

Can someone suggest a way to bypass this problem?

Thank you,
Nando

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

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

发布评论

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

评论(1

彻夜缠绵 2024-09-22 19:02:55

不,是 File.Open 锁定文件。

尝试将其投入使用:

using(var file = File.Open(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
   // process here
}

no, it is File.Open who lock files.

try to put it into using:

using(var file = File.Open(file.FullName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
   // process here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文