我有一个 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);
}
它可以工作,但由于某种原因它不显示某些快捷方式,它显示一些快捷方式,但大多数都没有显示。我不知道为什么会发生这种情况
最佳答案
您可能缺少“所有用户”桌面中的快捷方式:
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);
}
如果可行的话,您可以重构以组合公共(public)代码。
关于c# - DirectoryInfo.GetFiles() 不返回桌面上的所有文件(不包括快捷方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19932222/