File.Delete进程无法访问文件,因为它正在被另一个进程使用 [英] File.Delete the process cannot access the file because it is being used by another process

查看:219
本文介绍了File.Delete进程无法访问文件,因为它正在被另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共布尔DownloadMp3File(DownloadedMp3 mp3){WebClient客户端=新的WebClient();字符串filePath =";bool wasDownload = false;尝试 {字符串歌曲= mp3.SongName;filePath = @"mp3 \" +歌曲+".mp3";如果(File.Exists(filePath)){File.Delete(filePath);}DateTime tryCountNow = DateTime.Now;client = new WebClient();client.DownloadFileAsync(新的Uri(mp3.Url),filePath);client.DownloadProgressChanged + = client_DownloadProgressChanged;client.DownloadFileCompleted + = client_DownloadFileCompleted;DateTime开始= DateTime.Now;bool notDownload = false;downloadComplete = false;while(!downloadComplete){DateTime现在= DateTime.Now;TimeSpan ts = now-开始;int min = ts.Minutes;int sec = ts.Seconds;如果(10<秒&& 0 ==下载进度){notDownload = true;client.CancelAsync();休息;}如果(min == 1){notDownload = true;client.CancelAsync();休息;}线程睡眠(30);}如果(!notDownload){client.CancelAsync();client.OpenRead(mp3.Url);int downloadedFileSize = Convert.ToInt32(client.ResponseHeaders ["Content-Length"]);FileInfo localFile =新的FileInfo(filePath);如果(localFile.Length == downloadedFileSize){wasDownload = true;}}}抓住 {downloadProgress = 0;downloadComplete = false;}最后 {client.CancelAsync();client.Dispose();downloadComplete = false;downloadProgress = 0;GC.Collect();如果(!wasDownload){如果(File.Exists(filePath)){FileSecurity fs = File.GetAccessControl(filePath);File.Delete(filePath);}}Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,新动作(()=>MainWindow.label3.Content ="));}返回wasDownload;} 

请帮助!有时我会遇到例外情况:

File.Delete进程无法访问文件,因为它正在被另一个进程使用

我找不到原因(我处置了WebClient).

解决方案

您的代码建议您在新下载的文件上遇到正在使用的文件"异常.许多防病毒程序会自动扫描新创建和/或新下载的文件,并且可能会延迟关闭文件句柄,直到完成扫描为止.

如果这是您的问题,那么您别无选择,只能按时关闭文件.您可以切换到另一种不会在扫描过程中将文件锁定的防病毒软件,也可以在尝试使用最近关闭的文件时实施delay + retry循环.

public bool DownloadMp3File (DownloadedMp3 mp3) {
        WebClient client = new WebClient ();
        string filePath = "";
        bool wasDownload = false;
        try {


            string song = mp3.SongName;
            filePath = @"mp3\" + song + ".mp3";
            if (File.Exists (filePath)) {
                File.Delete (filePath);
            }

            DateTime tryCountNow = DateTime.Now;

            client = new WebClient ();
            client.DownloadFileAsync (new Uri (mp3.Url), filePath);
            client.DownloadProgressChanged += client_DownloadProgressChanged;
            client.DownloadFileCompleted += client_DownloadFileCompleted;
            DateTime start = DateTime.Now;
            bool notDownload = false;
            downloadComplete = false;
            while (!downloadComplete) {
                DateTime now = DateTime.Now;
                TimeSpan ts = now - start;
                int min = ts.Minutes;
                int sec = ts.Seconds;
                if (10 < sec && 0 == downloadProgress) {
                    notDownload = true;
                    client.CancelAsync ();
                    break;
                }
                if (min == 1) {
                    notDownload = true;
                    client.CancelAsync ();
                    break;
                }
                Thread.Sleep (30);
            }
            if (!notDownload) {
                client.CancelAsync ();
                client.OpenRead (mp3.Url);
                int downloadedFileSize = Convert.ToInt32 (client.ResponseHeaders["Content-Length"]);
                FileInfo localFile = new FileInfo (filePath);
                if (localFile.Length == downloadedFileSize) {
                    wasDownload = true;
                }
            }
        }
        catch {
            downloadProgress = 0;
            downloadComplete = false;
        }
        finally {
            client.CancelAsync ();
            client.Dispose ();
            downloadComplete = false;
            downloadProgress = 0;
            GC.Collect ();
            if (!wasDownload) {
                if (File.Exists (filePath)) {
                    FileSecurity fs = File.GetAccessControl (filePath);
                    File.Delete (filePath);
                }
            }
            Application.Current.Dispatcher.BeginInvoke (
            DispatcherPriority.Background,
            new Action (() =>
                MainWindow.label3.Content = ""
            ));
        }
        return wasDownload;
    }

Please help! I sometimes get that exception:

File.Delete the process cannot access the file because it is being used by another process

I can't find out why (I disposed WebClient).

解决方案

Your code suggests you're getting the "file being used" exception on a file that was newly downloaded. Many anti-virus programs automatically scan newly created and/or newly downloaded files, and may delay closing the file handle until the scan is done.

If that is your problem, then there's nothing more you can to do close the file on time. You can either switch to a different anti-virus that doesn't keep files locked during scans, or you can implement a delay+retry loop when trying to use a file that's recently been closed.

这篇关于File.Delete进程无法访问文件,因为它正在被另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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