Solved

detect network (shared) drive using C#

Posted on 2006-10-24
3
1,000 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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