如何使用c#删除临时文件? [英] How to Delete Temp Files using c#?

查看:154
本文介绍了如何使用c#删除临时文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我已经编写了删除临时文件的代码,但它运行不正常,因为它发出错误此文件无法访问,因为它是正在使用,我们如何解决这个问题。

我的代码是: -



  string  tempfolder = Path.GetTempPath(); 
string [] tempfiles = Directory.GetFiles(tempfolder, *。*,SearchOption.AllDirectories);

// MessageBox .Show(从{0}删除文件,tempfolder);

foreach string tempfile in tempfiles)
{
try
{
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(tempfolder);
foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories())subDirectory.Delete ( true );
// directory.Delete(true);
// File.Delete(tempfile);
}
catch (Exception ex)
{
Console.WriteLine( 文件可以不能删除:{0},tempfile);
}
}





请帮我解决这个问题。



先谢谢。



Ankit Agarwal

软件工程师

解决方案

你最好的解决方案是在File.Delete()调用周围放置一个Try / Ctach块。这将让您捕获您尝试删除的一个文件的错误并跳过它,继续删除枚举中的其余文件。子目录也是如此。

 foreach(Directory.GetFiles中的字符串filePath(tempPath,*。*,SearchOption.AllDirectories))
{
try
{
FileInfo currentFile = new FileInfo(filePath);
currentFile.Delete();
}
catch(exception ex)
{
Debug.WriteLine(文件错误:{0} \\\\ n {1},filePath,ex。信息);
}
}


请在删除前检查文件的状态,同时使用Using关键字,以便当它超出范围时,每个

变量声明都会释放。


你不能,你可以做的是把 try..catch 围绕删除

  foreach (System.IO.DirectoryInfo subDirectory  in  directory.GetDirectories())
尝试 {
subDirectory.Delete( true );
} catch {}



更好的方法是使用命令行:

 Process.Start(  cmd.exe  / c del / s / q \foldernamehere\); 


Hello,

I have write the code for Delete Temp Files but it is not running properly because it is giving an error "This file cannot access because it is being used", how can we solve this problem.
My code is:-

string tempfolder = Path.GetTempPath();
            string[] tempfiles = Directory.GetFiles(tempfolder, "*.*", SearchOption.AllDirectories);

            //MessageBox .Show ("Deleting files from {0}", tempfolder);

            foreach (string tempfile in tempfiles)
            {
                try
                {
                    System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(tempfolder);
                    foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
                   // directory.Delete(true);
                       // File.Delete(tempfile);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("File could not be deleted: {0}", tempfile);
                }
            }



Please help me, for this problem.

Thanks in Advance.

Ankit Agarwal
Software Engineer

解决方案

Your best solution is to put a Try/Ctach block around the File.Delete() call. This will let you catch the error on the one file you're trying to delete and skip it, continuing with deleting the remaining files in the enumeration. The same is true for subdirectories.

foreach (string filePath in Directory.GetFiles(tempPath, "*.*", SearchOption.AllDirectories))
{
    try
    {
        FileInfo currentFile = new FileInfo(filePath);
        currentFile.Delete();
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Error on file: {0}\r\n   {1}", filePath, ex.Message);
    }
}


Please check the state of the file before delete, also use "Using" keyword so that it every
variable declare will release when it is out of scope.


You can't, what you can do is put the try..catch around the Delete:

foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) 
try{ 
   subDirectory.Delete(true); 
} catch{}


A better way is to use the command line :

Process.Start("cmd.exe", "/c del /s /q \"foldernamehere\"");


这篇关于如何使用c#删除临时文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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