Solved

detect network (shared) drive using C#

Posted on 2006-10-24
3
998 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now