This is one of those examples when not being able to see the publication timestamp can lead you to make false conclusions...  So, “trust but verify”…

 

Say, you want to secure your application and disallow users from opening network files (or think of your own cases where you may need to call GetDriveType API, which determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive).  All web articles/posts I came across (as of July 21, 2006) indicated that you have to do P/Invoke as follows:

 

const int DRIVE_UNKNOWN = 0;

const int DRIVE_NO_ROOT_DIR = 1;

const int DRIVE_REMOVABLE = 2;

const int DRIVE_FIXED = 3;

const int DRIVE_REMOTE = 4;

const int DRIVE_CDROM = 5;

const int DRIVE_RAMDISK = 6;

 

[System.Runtime.InteropServices.DllImport("KERNEL32")]

public static extern int GetDriveType(string s);

 

public bool IsLocalDrive(string driveRoot)

{

    int driveType = GetDriveType(driveRoot);

 

    if (driveType == DRIVE_UNKNOWN)

        throw new ApplicationException(string.Format("Unable to determine drive type for {0}", driveRoot));

    else if (driveType == DRIVE_NO_ROOT_DIR)

        throw new ApplicationException(string.Format("Invalid argument {0}", driveRoot));

 

    return (driveType != DRIVE_REMOTE);

}

 

However, .NET 2.0 has DriveInfo support.  So, you can do same using .NET native code:

 

public bool IsLocalDrive(string path)

{

    bool result = false;

 

    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);

    System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();

    foreach (System.IO.DriveInfo driveInfo in drives)

    {

        if (string.Compare(driveInfo.RootDirectory.FullName, di.Root.FullName, true) == 0)

        {

            if (driveInfo.DriveType == System.IO.DriveType.Unknown)

                throw new ApplicationException(string.Format("Unable to determine drive type for {0}", path));

            else if (driveInfo.DriveType == System.IO.DriveType.NoRootDirectory)

                throw new ApplicationException(string.Format("Invalid argument {0}", path));

 

            result = (driveInfo.DriveType != System.IO.DriveType.Network);

 

            break;

        }

    }

 

    return result;

}