如何授予我的代码删除文件的权限

时间:2016-05-07 07:12:43

标签: c# .net visual-studio

我的代码删除了一些文件,有点奇怪,因为它没有给我任何错误。

基本上发生了什么,它没有删除文件夹中的任何文件(不知道为什么)我已经尝试授予它删除Only-Read文件的权限但是没有用。

我已经尝试过调试,但它没有向我显示任何内容。

我明白它无法删除正在其他地方使用的文件,我已经尝试手动删除一些文件并且有效,但不确定为什么我的代码不会自动执行,因为如果它遇到错误它应该继续下一个文件吗?

(这主要适用于.rar文件,但也适用于某些文件夹)

private void Clear_Click(object sender, EventArgs e)

{



    string tempPath = Path.GetTempPath();
    DirectoryInfo di = new DirectoryInfo(tempPath);

    foreach (DirectoryInfo dir in di.GetDirectories())
    {
        try
        {
            dir.Delete(true);
        }
        catch (Exception)
        {
            // Log error.
            continue;
        }
    }

    //-----------------------------------------------------------------------------------------------------

    //foreach (FileInfo file in di.GetFiles())
    foreach (FileInfo file in di.GetFiles())
    {
        try
        {
            file.Delete();
        }
        catch (Exception)
        {
            // Log error.
            continue;
        }
    }

    //-----------------------------------------------------------------------------------------------------
    // string[] extentionList = new[] { "*.jpg", "*.rar", "*.bmp", "*.gif" };

    //Delete RAR Files
    foreach (FileInfo file in di.GetFiles("*.rar"))
    {
        try
        {
            file.Delete();
        }
        catch
        { 
            //log error I tried running it without the "Continue;" but it still wouldnt log me an error.
            continue;
        }

        }

0 个答案:

没有答案
相关问题