简体   繁体   English

C#获取没有扩展名的文件的文件路径

[英]C# get file paths of just files with no extensions

I am wanting to get a string array of paths of files that do not have extensions. 我想获得一个没有扩展名的文件路径的字符串数组。 They are binary files with no extensions if that helps. 如果有帮助,它们是没有扩展名的二进制文件。

For example, I am loading a group of file paths out of a folder /test/ 例如,我正在从文件夹/test/加载一组文件路径

I want just the path and filenames that do not have a extension (so no .txt , no .csv , no .* ) 我只想要没有扩展名的路径和文件名(所以没有.txt ,没有.csv ,没有.*

/test/dontWant.txt

/test/dontWant.csv

/test/doWant

if i do: 如果我做:

String[] paths = Directory.GetFiles(fDir, "*.*", SearchOption.AllDirectories);

I of course get everything in those directories. 我当然得到那些目录中的所有内容。

if I then try: 如果我然后尝试:

String[] paths= Directory.GetFiles(fDir, "*", SearchOption.AllDirectories); 

I will still get everything in that directory. 我仍然会在该目录中获取所有内容。

Is there a way to just get the files of those that have no extension? 有没有办法只获取那些没有扩展名的文件?


using "*." "*." did work, and I don't know why I didn't try that to start with. 做了,我不知道为什么我没有尝试开始。

I should have been using EnumerateFiles to start with. 我应该一直使用EnumerateFiles开始。

You can try with this wildcard 您可以尝试使用此通配符

String[] paths = Directory.GetFiles(fDir, "*.", SearchOption.AllDirectories);

also you can use this wildcard with Directory.EnumerateFiles 您也可以将此通配符与Directory.EnumerateFiles

Directory.EnumerateFiles(fDir, "*.", SearchOption.AllDirectories);

This will help: 这将有助于:

var filesWithoutExtension = System.IO.Directory.GetFiles(@"D:\temp\").Where(filPath => String.IsNullOrEmpty(System.IO.Path.GetExtension(filPath)));
foreach(string path in filesWithoutExtension)
{
    Console.WriteLine(path);
}

It will return all the files w/o extension only in specified dir. 它将仅返回指定目录中没有扩展名的所有文件。 If you want to include all the sub-directories you'd have to use: System.IO.Directory.GetFiles(@"D:\\temp\\", "*", SearchOption.AllDirectories) . 如果要包含所有必须使用的子目录: System.IO.Directory.GetFiles(@"D:\\temp\\", "*", SearchOption.AllDirectories)

UPDATE UPDATE
As guys suggested, it's better to use Directory.EnumerateFiles because it consumes less ram. 正如大家建议的那样,最好使用Directory.EnumerateFiles因为它消耗的内存更少。

You will need to do a 2nd pass filter on it. 您需要对其进行第二遍过滤。

//If you are using .NET 3.5 you can still use GetFiles, EnumerateFiles will just use less ram.
String[] paths = Directory.EnumerateFiles(fDir, "*.*", SearchOption.AllDirectories)
                          .Where(file => Path.GetFileName(file) == Path.GetFileNameWithoutExtension(file))
                          .ToArray();

So what this does is it passes your file path to GetFileName and GetFileNameWithoutExtension , if both of those return the same string it then includes the result in the array. 所以它的作用是将文件路径传递给GetFileNameGetFileNameWithoutExtension ,如果两者都返回相同的字符串,则将结果包含在数组中。

As an alternative to aleksey.berezan's answer, you can do the following in .NET 4+. 作为aleksey.berezan的答案的替代方案,您可以在.NET 4+中执行以下操作。 EnumerateFiles will return files as they are traversed in the directory tree. EnumerateFiles将在目录树中遍历文件时返回文件。

foreach(var file in Directory.EnumerateFiles(fDir, "*.*", SearchOption.AllDirectories).Where(s => string.IsNullOrEmpty(Path.GetExtension(s))))
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM