문제

I have a listBox1 that should display all the files on my desktop, i have used the following method to do so

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

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

It works but for some reason it doesn't display some shortcuts, it displays a few shortcuts but most of them are not displayed. I have no idea why this is happening

도움이 되었습니까?

해결책

You are probably missing the shortcuts in the "All Users" desktop:

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);
}

You can refactor to combine the common code if that works.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top