删除另一个进程使用的文件
Posted
技术标签:
【中文标题】删除另一个进程使用的文件【英文标题】:Deletion of a file used by another processes 【发布时间】:2013-05-27 02:20:00 【问题描述】:我想删除这样的文件:
List<FichierModels> liste = fichiers.GetFichiersFromDirectoy(_directory_id);
//supprimer les fichiers expirés
foreach (Models.FichierModels fich in liste)
if (fich.Expiration_date < DateTime.Now)
System.IO.File.Delete(fich.Url);
fich.Delete_fichier(fich.Id);
public List<FichierModels> GetFichiersFromDirectoy(int _id_dir)
List<FichierModels> l = new List<FichierModels>();
Data.Connect();
using (Data.connexion)
string queryString = @"select Id, Id_directory, Url, Upload_date, Last_download, Downloads_count, Taille, Expiration_date, Name from Fichier where Id_directory = @Id_directory ";
SqlCommand command = new SqlCommand(queryString, Data.connexion);
command.Parameters.AddWithValue("@Id_directory", _id_dir);
try
SqlDataReader reader = command.ExecuteReader();
do
while (reader.Read())
FichierModels fichier = new FichierModels();
fichier.Id = reader.GetInt32(0);
fichier.Id_directory = reader.GetInt32(1);
fichier.Url = reader.GetString(2);
fichier.Upload_date = reader.GetDateTime(3);
fichier.Last_download = reader.GetDateTime(4);
fichier.Downloads_count = reader.GetInt32(5);
fichier.Taille = reader.GetDouble(6);
fichier.Expiration_date = reader.GetDateTime(7);
fichier.Name = reader.GetString(8) ;
l.Add(fichier);
while (reader.NextResult());
return l;
catch return null;
我用过这个功能:
public void ExtractArchive(string zipFilename, string ExtractDir)
ZipInputStream zis = null;
FileStream fos = null;
try
zis = new ZipInputStream(new FileStream(zipFilename, FileMode.Open, FileAccess.Read));
ZipEntry ze;
// on dezippe tout dans un rep du nom du zip, pas en bordel
Directory.CreateDirectory(ExtractDir);
while ((ze = zis.GetNextEntry()) != null)
if (ze.IsDirectory)
Directory.CreateDirectory(ExtractDir + "\\" + ze.Name);
else
if (!Directory.Exists(ExtractDir + "\\" + Path.GetDirectoryName(ze.Name)))
Directory.CreateDirectory(ExtractDir + "\\" + Path.GetDirectoryName(ze.Name));
fos = new FileStream(ExtractDir + "\\" + ze.Name, FileMode.Create, FileAccess.Write);
int count;
byte[] buffer = new byte[4096];
while ((count = zis.Read(buffer, 0, 4096)) > 0)
fos.Write(buffer, 0, count);
finally
if (zis != null) zis.Close();
if (fos != null) fos.Close();
但我有一个异常表明该文件被另一个进程使用。如何暂停此过程,删除文件然后继续该过程?
【问题讨论】:
您最好尝试了解锁定此文件的进程以及原因。 Process explorer 可能会有所帮助!杀死线程或进程可能会比解决您的问题造成更大的损害。 How do I delete a file which is locked by another process in C#?的可能重复 能否也添加GetFichiersFromDirectoy
方法的主体?
@AlexFilipovici 查看我的编辑
您是否很可能从自己的应用程序中锁定了文件?
【参考方案1】:
您可以使用this 程序来检查哪个进程锁定了您要删除的文件。您需要终止锁定文件的进程才能删除文件。 一些杀死进程的示例代码:
Process tool = new Process();
tool.StartInfo.FileName = "handle.exe";
tool.StartInfo.Arguments = fileName+" /accepteula";
tool.StartInfo.UseShellExecute = false;
tool.StartInfo.RedirectStandardOutput = true;
tool.Start();
tool.WaitForExit();
string outputTool = tool.StandardOutput.ReadToEnd();
string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
foreach(Match match in Regex.Matches(outputTool, matchPattern))
Process.GetProcessById(int.Parse(match.Value)).Kill();
用你的模式替换 matchPattern,你应该能够杀死锁定你文件的进程。
【讨论】:
【参考方案2】:暂停进程对您没有帮助。您必须停止在进程中使用该文件,或者如果您无法关闭该进程。您正在使用网络界面,因此您可以向用户显示一条消息,哪个文件阻止删除文件,并让他选择重试。如果文件被解锁(进程释放锁或进程关闭)删除将成功。如果没有,他可以重试。
有人认为您应该问自己为什么需要删除仍在使用的文件。我觉得有点奇怪。
【讨论】:
请看我的编辑,也许我在关闭文件时出错了【参考方案3】:我不知道是不是你的情况,但有时我会遇到像你这样的异常,因为我在同一个进程中打开文件时没有调用方法 Close()。尝试检查您第一次使用该文件的位置
【讨论】:
这应该是评论,我相信 请看我的编辑,也许我在关闭文件时出错了以上是关于删除另一个进程使用的文件的主要内容,如果未能解决你的问题,请参考以下文章