Solved

detect network (shared) drive using C#

Posted on 2006-10-24
3
1,004 Views
Last Modified: 2010-05-18
How can I determine if a drive is mapped or local, using C#? I am using .NET Framework 1.1 with VS.NET 2003.

thanks
guid
0
Comment
Question by:guidway
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 17805617
On version 1.1 of the Framework, you can use:

  static bool IsNetworkDrive(char driveLetter)
  {
    const int NetworkDrive = 4;

    System.Management.ObjectQuery query = new System.Management.ObjectQuery("SELECT * FROM Win32_LogicalDisk");
    System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);
    System.Management.ManagementObjectCollection drives = searcher.Get();

    foreach (System.Management.ManagementBaseObject drive in drives)
      if (drive.Properties["DeviceID"].Value.ToString()[0] == driveLetter)
        return Convert.ToInt32(drive.Properties["DriveType"].Value) == NetworkDrive;

    throw new ArgumentException();
  }

And when you upgrade to version 2.0:

  static bool IsNetworkDrive(char driveLetter)
  {
    foreach (System.IO.DriveInfo drive in System.IO.DriveInfo.GetDrives())
      if (drive.ToString()[0] == driveLetter)
        return drive.DriveType == System.IO.DriveType.Network;

    throw new ArgumentException();
  }
0
 
LVL 12

Author Comment

by:guidway
ID: 17812179
Excellent! That's more than I expected, but information I needed ( we are about to upgrade to 2.0).

Increasing to 500.
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17813345
Thank you, guidway!
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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