Title: List the shortcuts in the computer's network neighborhood in C#
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 the example to experiment with it and to see additional details.
|