List the shortcuts in the computer’s network neighborhood in C#

[network neighborhood]

This example uses the Windows Script Host to list shortcuts in the network neighborhood. Before the program can use the Windows Script Host, you must add a reference to the COM object “Windows Script Host Object Model.” To make using that library easier, the program includes the following two using directives.

using System.IO;
using IWshRuntimeLibrary;

The following code shows how the program builds its list of shortcuts.

private void Form1_Load(object sender, EventArgs e)
{
    // Make a Windows Script Host Shell object.
    IWshShell_Class wsh_shell = new IWshShell_Class();

    // Find the Nethood folder.
    IWshCollection special_folders = wsh_shell.SpecialFolders;
    object path_name = "Nethood";
    string nethood_path =
        special_folders.Item(ref path_name).ToString();
    DirectoryInfo di = new DirectoryInfo(nethood_path);

    // Enumerate Nethood's subdirectories.
    foreach (DirectoryInfo subdir in di.GetDirectories())
    {
        lstLinks.Items.Add(subdir.Name);
    }
}

The code creates an IWshShell_Class object and gets the Nethood entry from its SpecialFolders collection. It converts that value into a string to get the path to the network neighborhood folder. It then makes a DirectoryInfo object representing that folder.

Finally, the code loops through the folder’s subdirectories, listing them in the lstLinks ListBox.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in miscellany, network and tagged , , , , , , , , , , , . Bookmark the permalink.

2 Responses to List the shortcuts in the computer’s network neighborhood in C#

  1. Pingback: Список ярлыков в сетевом окружении компьютера в C#

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.