删除C#中文件以外的目录中的所有内容

llk*_*llk 6 c# file delete-file

我在删除除文件(index.dat)之外的目录中的所有内容时遇到问题我正在尝试清除cookies文件夹和文件的临时文件夹但是当我尝试删除index.dat时出现错误,因为它被另一个文件使用处理.有没有办法删除temp和cookies文件夹中 index.dat文件以外的所有内容?这是我的代码:

string userProfile = Environment.GetEnvironmentVariable("USERPROFILE");
string strDirLocalq = Path.Combine(userProfile, "AppData");
string strDirLocalw = Path.Combine(strDirLocalq, "Roaming");
string strDirLocale = Path.Combine(strDirLocalw, "Microsoft");
string strDirLocalr = Path.Combine(strDirLocale, "Windows");
string strDirLocalt = Path.Combine(strDirLocalr, "Cookies");

string[] filePaths = Directory.GetFiles(strDirLocalt);
foreach (string filePath in filePaths)
    File.Delete(filePath);
Run Code Online (Sandbox Code Playgroud)

Pol*_*Pol 11

这有效:

string[] filePaths = Directory.GetFiles(strDirLocalt);
foreach (string filePath in filePaths)
{
    var name = new FileInfo(filePath).Name;
    name = name.ToLower();
    if (name != "index.dat")
    {
        File.Delete(filePath);
    }
}
Run Code Online (Sandbox Code Playgroud)