views:

313

answers:

4

Is there a way, within the .net framework, to check to see if two different shared folders are actually pointing to the same physical directory? Do directories in Windows have some sort of unique identifier? Google-fu is failing me.

(I mean, aside from writing a temp file to one and seeing if it appears in the other)

Edit: I think I've discovered what I need,with thanks to Brody for getting me pointed in the right direction in the System.Management namespace.

A: 

You can examine the share definition itself by using the System.Management namespace but it is not easy to use.

it starts something like

ManagementClass management = new ManagementClass("\\\\.\\root\\cimv2", "Win32_Share", null)

And it gets much worse after that. I have used it to create a share. Hopefully you can use it to the path for each share and compare.

Brody
A: 

I think the .NET framework doesn't provide the information you need for comparing 2 directories ... You have to take an unmanaged approach. This is how I did it:

class Program
{
    struct BY_HANDLE_FILE_INFORMATION
    {
        public uint FileAttributes;
        public System.Runtime.InteropServices.ComTypes.FILETIME CreationTime;
        public System.Runtime.InteropServices.ComTypes.FILETIME LastAccessTime;
        public System.Runtime.InteropServices.ComTypes.FILETIME LastWriteTime;
        public uint VolumeSerialNumber;
        public uint FileSizeHigh;
        public uint FileSizeLow;
        public uint NumberOfLinks;
        public uint FileIndexHigh;
        public uint FileIndexLow;
    }

    //
    // CreateFile constants
    //
    const uint FILE_SHARE_READ = 0x00000001;
    const uint FILE_SHARE_WRITE = 0x00000002;
    const uint FILE_SHARE_DELETE = 0x00000004;
    const uint OPEN_EXISTING = 3;

    const uint GENERIC_READ = (0x80000000);
    const uint GENERIC_WRITE = (0x40000000);

    const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
    const uint FILE_READ_ATTRIBUTES = (0x0080);
    const uint FILE_WRITE_ATTRIBUTES = 0x0100;
    const uint ERROR_INSUFFICIENT_BUFFER = 122;
    const uint FILE_FLAG_BACKUP_SEMANTICS = 0x02000000;


    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr CreateFile(
        string lpFileName,
        uint dwDesiredAccess,
        uint dwShareMode,
        IntPtr lpSecurityAttributes,
        uint dwCreationDisposition,
        uint dwFlagsAndAttributes,
        IntPtr hTemplateFile);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool GetFileInformationByHandle(IntPtr hFile, out BY_HANDLE_FILE_INFORMATION lpFileInformation);

    static void Main(string[] args)
    {
        string dir1 = @"C:\MyTestDir";
        string dir2 = @"\\myMachine\MyTestDir";
        Console.WriteLine(CompareDirectories(dir1, dir2));
    }

    static bool CompareDirectories(string dir1, string dir2)
    {
        BY_HANDLE_FILE_INFORMATION fileInfo1, fileInfo2;
        IntPtr ptr1 = CreateFile(dir1, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING,  FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
        if ((int)ptr1 == -1)
        {
            System.ComponentModel.Win32Exception t = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
            Console.WriteLine(dir1 + ": " + t.Message);
            return false;
        }
        IntPtr ptr2 = CreateFile(dir2, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING,  FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero);
        if ((int)ptr2 == -1)
        {
            System.ComponentModel.Win32Exception t = new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
            Console.WriteLine(dir2 + ": " + t.Message);
            return false;
        }
        GetFileInformationByHandle(ptr1, out fileInfo1);
        GetFileInformationByHandle(ptr2, out fileInfo2);

        return ((fileInfo1.FileIndexHigh == fileInfo2.FileIndexHigh) &&
            (fileInfo1.FileIndexLow == fileInfo2.FileIndexLow));
    }
}

It works! Hope this helps.

Cheers.

bruno conde
A: 

I believe using WMI queries will take care of what I need to do:

Connection options = new ConnectionOptions();
ManagementScope scpoe = new ManagementScope("\\\\Server\\root\\cimv2", options);
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Share WHERE Name = '" + name +"'")

ManagementObjectSearcher searcher = new ManagementObjectSearch(scope, query);
ManagementObjectCollection qc = searcher.Get();

foreach (ManagementObject m in qc) {
    Console.WriteLine(m["Path"]);
}

And the Path attribute will give me the physical path of the share, which I can use to compare the two shares.

Dana
+1  A: 

If you don't go WMI, the unmanaged call is NetShareEnum with a servername of NULL (local computer) and a level of 502 to get a SHARE_INFO_502 struct. The local path is in shi502_path.

P/Invoke info, as always, is over at pinvoke.net.

Mark Brackett