Search code examples
c#filterms-wordtemporary-files

How to filter word temp file


Right now, I'm developing a winform system.I want to filter word temp file or temp filename.When someone open a word file ,in my system he will see the temp file .

And then doing some operation with temp file, the system will throw an Exception .

I just want filter the file and don't throw the Exception .


Solution

  • This worked for me:

    var files = new DirectoryInfo(@"C:\Users\Alex\Desktop").GetFiles()
        .Where(arg => !arg.Attributes.HasFlag(FileAttributes.Hidden) || arg.Extension != ".docx")
        .ToList();
    

    You can also add limiting by file name prefix:

    var files = new DirectoryInfo(@"C:\Users\Alex\Desktop").GetFiles()
        .Where(arg => !(arg.Attributes.HasFlag(FileAttributes.Hidden) && arg.Extension == ".docx" && arg.Name.StartsWith("~$")))
        .ToList();