.Net file collects unwanted files (C #)

I have a command as shown below. I believe that if I use the *.csv file template, it also picks up elements with the .csvx extension. Maybe this is a return to days of file names in 8.3 format: does anyone know a way that will return them properly, preferably without folding our own?

 files = (from file in Directory.EnumerateFiles(sourceFolder, filePattern, SearchOption.TopDirectoryOnly) select file).ToList(); 
+6
source share
4 answers

Just a workaround, but may be good enough:

 var files = Directory .EnumerateFiles(sourceFolder, filePattern, SearchOption.TopDirectoryOnly) .Where(f => f.EndsWith(".csv")) .ToList(); 
+5
source

You can try something like this:

 var files = (from file in Directory.EnumerateFiles(directory, "*.csv", SearchOption.TopDirectoryOnly) select file).Where(c => c.EndsWith(".csv")).ToList(); 
+3
source

You can also use the "Extension" property

 var files = new DirectoryInfo(path).GetFiles("*.csv").Where((info) => info.Extension = ".csv") 
+1
source

This is by design:

Since this method checks the file names as with the 8.3 file name format and long file name format, a search pattern similar to "1.txt" may return unexpected file names. For example, using the search template “1.txt” returns “longfilename.txt” because the equivalent file format 8.3 is: “LONGFI ~ 1.TXT”.

You will have to use one of the workarounds.

Source: http://msdn.microsoft.com/en-us/library/wz42302f.aspx

+1
source

Source: https://habr.com/ru/post/905150/


All Articles