DirectoryInfo.GetFiles()没有返回桌面上的所有文件(它不包括快捷方式)

Joh*_*ohn 2 .net c# directoryinfo getfiles

我有一个listBox1应该显示我桌面上的所有文件,我使用了以下方法来执行此操作

string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
DirectoryInfo path = new DirectoryInfo(filepath);

foreach (var file in path.GetFiles())
{
    listBox1.Items.Add("File : " + file.Name);
}
Run Code Online (Sandbox Code Playgroud)

它可以工作,但由于某种原因,它不显示一些快捷方式,它显示一些快捷方式,但大多数都没有显示.我不知道为什么会这样

D S*_*ley 6

您可能缺少"所有用户"桌面中的快捷方式:

string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
DirectoryInfo path = new DirectoryInfo(filepath);

foreach (var file in path.GetFiles())
{
    listBox1.Items.Add("File : " + file.Name);
}

//  Get files in the "common" desktop
filepath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory);
path = new DirectoryInfo(filepath);

foreach (var file in path.GetFiles())
{
    listBox1.Items.Add("File : " + file.Name);
}
Run Code Online (Sandbox Code Playgroud)

如果有效,您可以重构以组合公共代码.