Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获得C语言中的所有文件扩展名?_C#_Directory - Fatal编程技术网

C# 如何获得C语言中的所有文件扩展名?

C# 如何获得C语言中的所有文件扩展名?,c#,directory,C#,Directory,是否有方法检查enterf exstention是否存在?一个文件可以有它想要的任何扩展名。它可以是任意长度的任意字符序列。因为这个序列是无限的,所以没有办法把它们全部放在一个列表中 如果您想知道您感兴趣的目录是否有任何具有给定扩展名的文件,可以枚举目录中的所有文件,并将其所有扩展名放入一个集合。您可以使用Linq尝试此操作: string path = "C:\\BSD"; string extension = Console.ReadLine(); List<string> al

是否有方法检查enterf exstention是否存在?

一个文件可以有它想要的任何扩展名。它可以是任意长度的任意字符序列。因为这个序列是无限的,所以没有办法把它们全部放在一个列表中


如果您想知道您感兴趣的目录是否有任何具有给定扩展名的文件,可以枚举目录中的所有文件,并将其所有扩展名放入一个集合。

您可以使用Linq尝试此操作:

string path = "C:\\BSD";
string extension = Console.ReadLine();
List<string> allExstensions = getAllExtention(); // Is there a method where I get all File Extensions : *.png, *.txt,.......
if (!allExstensions.Contains(extension))
    throw new Exception("The Extension you wrote does not exist!!");

foreach (string directory in Directory.GetDirectories(path))
{
    foreach (string file in Directory.GetFiles(directory,extension))
    {
        Console.WriteLine(file);
    }
}

不,文件可以有任意扩展名,也可以没有扩展名。有一个注册扩展名的概念,但是您不需要注册文件扩展名就可以使用它。您想要实现什么?你认为哪些扩展是有效的?你的问题很不清楚。
    string path = "C:\\BSD";
    string extension = Console.ReadLine();
    int count = 0;

    foreach (string file in Directory.GetFiles(path, "*.*",SearchOption.AllDirectories).Where(x => x.EndsWith(extension)))
    {
       Console.WriteLine(file);
       count++;
    }

    if (count == 0)
      throw new Exception("The Extension you wrote does not exist!!");