views:

723

answers:

2

Once upon a time I read how you detect programmatically for mounted NTFS folders (can cause cyclic recursion when searching through folders). Now i cannot find the link.. Does anyone know how to do this?

The mounting I am interested in detecting is when one folder is mounted to another folder.

+1  A: 

I assume you mean a NTFS junction? There is an unmanaged API to get the reparse point, which you then have to interrogate to see if it's actually a junction. This is all available through P/Invoke, of course.

But, most folks just(1) look for ReparsePoint on the list of attributes returned by DirectoryInfo.GetDirectories.

(1) Note that a NTFS Junction is a particular type of reparse point, but not the only one. Symbolic links, hard links,(2) and any other user defined data are also reparse points.

(2) Whoops. Hard links aren't reparse points, they're just standard directory entries pointing to the same file. Thanks to Reuben for correcting me on that.

Mark Brackett
As Mark hints, "skipping" all reparse points during recursive directory enumeration can be a bad idea. Depending on your context, it might be a wise idea to carefully exclude symlinks and junctions.Also, a minor correction to Mark's comment--hard links are *not* implemented as reparse points.
Reuben
@Reuben - You are correct, hard links are *not* reparse points. Edited and thanks for the correction.
Mark Brackett
A: 

Do this via WMI. See sample at : http://msdn.microsoft.com/en-us/library/aa393244(VS.85).aspx

Or try this sample code made with WMI Code Creator:


using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_DiskPartition"); 

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_DiskPartition instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Type: {0}", queryObj["Type"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}
lsalamon