Seaky
Seaky

Reputation: 249

Delete TMP file in a local folder

I have these files in my local folder.

enter image description here

As you can see from the image above, there is a TMP file in it. I don't know how it is generated but I believe it is a useless temp file, so I try to delete it this way:

        foreach (var item in await ApplicationData.Current.LocalFolder.GetFilesAsync())
            if (item.Name.EndsWith(".TMP"))
                await item.DeleteAsync();

However, using neither GetFilesAsync() nor GetItemsAsync() finds the temp file. The former gives me the json files only and the latter finds everything but the tmp file.

How should I find and delete it?

Upvotes: 0

Views: 169

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

You could use the following code to detect the hidden file where in the sandbox(It only works in sandbox).

public static class ParseDir
{
    public static FileInfo[] GetFilesFromDirectory(string DirName, string pattern, bool Recursive)
    {
        if (!Directory.Exists(DirName))
            throw new Exception("No such Directory.");
        DirectoryInfo dirInfo = new DirectoryInfo(DirName);
        SearchOption Recur = Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
        return dirInfo.GetFiles(pattern, Recur);
    }

    public static FileInfo[] GetHiddenOnlyFiles(FileInfo[] Files)
    {
        List<FileInfo> result = new List<FileInfo>();
        foreach (FileInfo file in Files)
            if ((file.Attributes & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden)
                result.Add(file);
        return result.ToArray();
    }

}

Usage

 FileInfo[] filesInS = ParseDir.GetFilesFromDirectory(ApplicationData.Current.LocalFolder.Path, "*.*", false);
 FileInfo[] hiddenFiles = ParseDir.GetHiddenOnlyFiles(filesInS);
 hiddenFiles.First().Delete();

Upvotes: 1

Related Questions