确定是否在.NET中共享文件夹

有没有办法通过.NET框架来确定文件夹是否共享?

Diretory,DirectoryInfo或FileAttributes都没有任何相应的字段。

我忘记提到的一件事是,我想要检查networking共享。 但我会调查WMI的东西。

Solutions Collecting From Web of "确定是否在.NET中共享文件夹"

您可以使用WMI Win32_Share。 看一眼:

http://www.gamedev.net/community/forums/topic.asp?topic_id=408923

显示查询,创建和删除共享文件夹的示例。

您可以使用WMI Win32_Share获取所有共享文件夹的列表,并查看您要查找的文件夹是否位于它们之间。 这里有一个片段可以帮助你:

public static List<string> GetSharedFolders() { List<string> sharedFolders = new List<string>(); // Object to query the WMI Win32_Share API for shared files... ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from win32_share"); ManagementBaseObject outParams; ManagementClass mc = new ManagementClass("Win32_Share"); //for local shares foreach (ManagementObject share in searcher.Get()){ string type = share["Type"].ToString(); if (type == "0") // 0 = DiskDrive (1 = Print Queue, 2 = Device, 3 = IPH) { string name = share["Name"].ToString(); //getting share name string path = share["Path"].ToString(); //getting share path string caption = share["Caption"].ToString(); //getting share description sharedFolders.Add(path); } } return sharedFolders; } 

请注意,我粗暴地从这个链接复制粘贴字节

另外一种方法来剥皮这个猫是使用powershell(如果你有它安装)来调用wmi调用,包括对System.Management.Automation的引用,它将大部分可能在\ program files \ reference assemblies \ microsoft \ windowspowershell

 private void button1_Click(object sender, EventArgs e) { Runspace rs = RunspaceFactory.CreateRunspace(); rs.Open(); Pipeline pl = rs.CreatePipeline(); pl.Commands.AddScript("get-wmiobject win32_share"); StringBuilder sb = new StringBuilder(); Collection<PSObject> list = pl.Invoke(); rs.Close(); foreach (PSObject obj in list) { string name = obj.Properties["Name"].Value as string; string path = obj.Properties["Path"].Value as string; string desc = obj.Properties["Description"].Value as string; sb.AppendLine(string.Format("{0}{1}{2}",name, path, desc)); } // do something with the results... } 

尝试使用WMI并执行SELECT * FROM Win32_ShareToDirectory查询。