Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I identify if a folder is local or network in C#

Status
Not open for further replies.

randall2nd

Programmer
May 30, 2001
112
US
I need to be able to Identify if a given folder resides on a mapped network drive or the local machine.

I know in VB there is the "Scripting.FileSystemObject" that has a "Drive.DriveType" object that provides exactly what I am looking for. Is there an equivalent in C#?

Currently I am just checking if the drive letter is greater than "E". Obviously this is an incredibly flawed and bad way of doing this.

I am supposed to be using the .NET framework, but anything will be better than the way I am doing it now.

Please Help

Randall2nd
 
Figured it out!!!!
Here is what worked for me.

This requires the System.Management Namespace. It was not included in my normal install, but you can reference it by clicking "Project|Add Reference" from the menu in Visual Studio.

Code:
using System.Management;
[...]
private class DriveTypes
{
  public const int Unknown = 0;
  public const int Not_Root_Directory = 1;
  public const int Removable_Disk = 2;
  public const int Local_Disk = 3;
  public const int Network = 4;
  public const int Compact_Disc = 5;
  public const int RAM_Disk = 6;
}
[...]
ManagementObject CurrDriveInfo = 
   new ManagementObject("Win32_LogicalDisk='" + dirPath.Substring(0,dirPath.IndexOf(@"\")) + "'");
try
{
   //check if Network drive
  if(int.Parse(CurrDriveInfo.GetPropertyValue("DriveType").ToString()) == DriveTypes.Network)
    {isNetWork  = true;}
  else
    {isNetWork  = false;}
}
catch(System.Management.ManagementException)
{isNetWork  = false; // error indicates that drive does not exist}

I only cared whether or not it was a network drive, so that is all I checked for. Otherwise should probably use a switch..case statement.

If you know a better way or I got a type wrong post back and let me know.

Randall2nd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top