Somehow a file has appeared in one of my directories, and it has space at the end of its extension -
its name is "test.txt ". The weird thing is that Directory.GetFiles()
returns me the path of this
file, but I'm unable to retrieve file information with FileInfo
class.
The error manifests here:
DirectoryInfo di = new DirectoryInfo("c:\\somedir");
FileInfo fi = di.GetFileSystemInfos("test*")[0] as FileInfo;
//correctly fi.FullName is "c:\somedir\test.txt "
//but fi.Exists==false (!)
Is FileInfo class broken? Can I somehow retrieve information about this file? I really don't know how did that file appear on my file system, and I am unable to recreate some more of them.
All of my attempts to create a new file with this type of extension have failed, but now my program is crashing when encoutering it. I can easily handle the exception when finding the file, but boy am I curious about this!
Yes i know of these files. I also got once such a beast thing. To get rid of it i don't know about a programming way in C#, but good old command line is your friend:
Open a console window in the given folder (or execute cmd and navigate to the folder with
cd
command). Now enterdir /x
to retrieve the shortname of the files in this directory. Use this name to delete or rename the file by using thedel
orren
command.Ending file names with a space is documented as a Bad Idea.
From MSDN "Naming Files, Paths, and Namespaces (Windows)":
Also, the KB article "INFO: Filenames Ending with Space or Period Not Supported":
DirectoryInfo
probably usesFindFirstFile()
and friends to produce directory listings.File.Exists
is most likely implemented throughGetFileAttributes()
which probably suffers from the same problem asCreateFile()
and will report a nonexistent file.Hence, not a problem in .NET specifically, but in Windows itself.