在.net框架内,是否有办法检查两个不同的共享文件夹是否实际指向同一个物理目录? Windows中的目录是否具有某种唯一标识符? Google-fu让我失望。
(我的意思是,除了将临时文件写入一个并查看它是否出现在另一个中)
编辑:我想我已经发现了我需要的东西,感谢Brody让我指向System.Management命名空间中的正确方向。
答案 0 :(得分:1)
如果你没有去WMI,那么非托管调用是NetShareEnum,服务器名为NULL(本地计算机),级别为502,以获得SHARE_INFO_502结构。本地路径位于shi502_path。
P/Invoke info一如既往地在pinvoke.net上结束。
答案 1 :(得分:0)
您可以使用System.Management命名空间检查共享定义本身,但它不容易使用。
它开始像
ManagementClass management = new ManagementClass("\\\\.\\root\\cimv2", "Win32_Share", null)
之后情况变得更糟。我用它来创建一个共享。希望您可以将它用于每个共享的路径并进行比较。
答案 2 :(得分:0)
我认为.NET框架不提供比较2个目录所需的信息...... 你必须采取一种不受管理的方法。我就这样做了:
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));
}
}
有效!希望这会有所帮助。
干杯。
答案 3 :(得分:0)
我相信使用WMI查询会照顾我需要做的事情:
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"]);
}
Path属性将为我提供共享的物理路径,我可以用它来比较两个共享。