Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List EVERYTHING in Desktop folder

So here's the thing... I'm making a small app that should be able to list EVERYTHING on a users Desktop - including shortcuts.

So I was doing this:

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

            foreach (var file in d.GetFiles())
            {
                Console.WriteLine(file.Name);
            }

That gives me the following:

 Spotify.lnk  
 Desktop.ini

But on my Desktop I can see these:

Spotify.lnk
Desktop.ini
Microsoft Office 2010
VLC Media Player

So I tried to pull some WMI info from: Win32_ShortcutFile without any luck. (It lists stuff I don't have on the desktop like Windows Live.)

So at the moment I'm kind of clueless...

I hope this made any sense!

Any pointers in the right direction would be awesome!

Cheers.

EDIT: I forgot to mentioned - the target node is a Windows Server 2008 SP1 machine.

EDIT: I also forgot to mention that I am already checking for folders on the desktop.

like image 736
RobinNilsson Avatar asked Jan 28 '13 22:01

RobinNilsson


3 Answers

You need to check the public user's desktop.

In .Net 4.0 and above, you can use the Environment.SpecialFolder.CommonDesktopDirectory special folder to get at that directory.

On your machine it is probably C:\Users\Public\Desktop if you have not changed it. If you look in there, you should see the files that are missing from the C:\Users\YourUserName\Desktop folder.

If you are on .net 3.5 or below, then the CommonDesktopDirectory does not exist in the special folder enum. If that is the case, you will need to use a Win32 API call to get the folder path.

[DllImport("shfolder.dll", CharSet = CharSet.Auto)]
private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
private const int MAX_PATH = 260;
private const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019;
public static string GetAllUsersDesktopFolderPath()
{
    StringBuilder sbPath = new StringBuilder(MAX_PATH);
    SHGetFolderPath(IntPtr.Zero, CSIDL_COMMON_DESKTOPDIRECTORY, IntPtr.Zero, 0, sbPath);
    return sbPath.ToString();
}
like image 200
John Koerner Avatar answered Oct 01 '22 06:10

John Koerner


Also you need to scan this directory:

string filepath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory);
like image 23
Sergey Vyacheslavovich Brunov Avatar answered Oct 01 '22 05:10

Sergey Vyacheslavovich Brunov


If you want to get All destop items you will have to check DesktopDirectory and CommonDesktopDirectory

    var list = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)).GetFiles()
       .Concat(new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory)).GetFiles())
       .Distinct();

    foreach (var file in list)
    {
        Console.WriteLine(file.Name);
    }
like image 33
sa_ddam213 Avatar answered Oct 01 '22 05:10

sa_ddam213