?
Solved

Location of network drives in C#

Posted on 2006-05-23
3
Medium Priority
?
498 Views
Last Modified: 2012-08-14
THere is several network drives mapped on the system. I need to create C# application which will display the drive letter, server name, and the share directory which its connected to. Some thing like output of "net use". ANy ideas on how to do it? Simple examples are welcome.
0
Comment
Question by:g00r00
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
neilprice earned 1000 total points
ID: 16754447
Not sure if this is exactly what you want but try this:

using System;
using System.IO;
using System.Management;

class Test
{
    public static void Main(string[] args)
    {
        ManagementObjectSearcher DiskSearch = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");

        foreach (ManagementObject disk in DiskSearch.Get())
        {
            if (disk["DriveType"].ToString() == "4") // Type 4 is a network drive
            {
                Console.WriteLine(disk["Name"].ToString()); // Drive letter
                Console.WriteLine(disk["ProviderName"].ToString()); // Network ID
                DirectoryInfo[] dirs = new DirectoryInfo(disk["ProviderName"].ToString()).GetDirectories(); // List directories
                foreach (DirectoryInfo d in dirs)
                {
                    Console.WriteLine(d.FullName.ToString());
                }
            }
        }
        Console.ReadLine();
    }
}

Hope this helps,
Neil
0
 
LVL 6

Expert Comment

by:neilprice
ID: 16754452
Forgot to say you'll probably need to add a reference to System.Management for that to work.

Neil
0
 

Author Comment

by:g00r00
ID: 16755011
THANKS,,, IT DOES WHTAT I WANT IT TO DO,,,,

ManagementObjectSearcher DiskSearch = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
      foreach (ManagementObject disk in DiskSearch.Get())
            {
                  if (disk["DriveType"].ToString() == "4") // Type 4 is a network drive
                  {
                        Console.WriteLine(disk["Name"].ToString()+" ---      "+disk["ProviderName"].ToString()); // Drive letter & network ID
                        }
                  }                  
            Console.WriteLine();
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question