c# 有没有一种方法可以在数组中没有路径和扩展名的情况下获取多个文件名?

c# is there a way to get more than one files name without their path and their extension in an array?

简而言之,我想要做的是将某个文件夹中的文件名称作为数组获取,就像底部的示例一样,但是它获取完整路径而不仅仅是文件名,也保留我不想保留的 .lnk 部分。

string[] directory = Directory.GetFiles(@"C:\Program Files (x86)\programloc\shortcuts","*.lnk");

我想知道我是否可以像我做完整路径那样做,但是由于 snytax,它没有按照我希望的方式工作。

// set the directory here to "C:\Program Files (x86)\programloc\shortcuts" call it as "string paths;"
//string[] file = paths.GetFiles("*.lnk");??????

foreach (string dir in directory)
        {
         //adding "Console.WriteLine(file);" in here should give me the .lnk files in that folder without their path or extension
        }

您可以在 LinqPath.GetFileNameWithoutExtension

上使用一点
string[] directory = Directory.GetFiles(@"C:\Program Files (x86)\programloc\shortcuts", "*.lnk")
    .Select(System.IO.Path.GetFileNameWithoutExtension)
    .ToArray();