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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/136539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:04:37  来源:igfitidea点击:

Determining if a folder is shared in .NET

提问by Dana

Is there a way through the .net framework to determine if a folder is shared or not?

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

Neither Diretory, DirectoryInfo or FileAttributes seem to have any corresponding field.

Diretory、DirectoryInfo 或 FileAttributes 似乎都没有任何相应的字段。

One thing I forgot to mention was that I want to be checking for network shares. But I'll investigate the WMI stuff.

我忘记提及的一件事是我想检查网络共享。但我会调查 WMI 的东西。

采纳答案by Yuval Peled

You can use WMI Win32_Share. Take a look at:

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

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

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

Shows a sample for querying, creating and deleting shared folders.

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

回答by benPearce

Try using WMI and doing a SELECT * FROM Win32_ShareToDirectoryquery.

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

回答by JohnIdol

You can get a list of all the shared folders using the WMI Win32_Share and see if the folder you're looking for is between them. Here's a snippet that might help you with this:

您可以使用 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;

}

Please note that I brutally copy-pasted from this linkon bytes

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

回答by Tim Jarvis

One more way to skin this cat is to use powershell (if you have it installed) to invoke the wmi call, include a reference to System.Management.Automation, it will most likley be in \program files\reference assemblies\microsoft\windowspowershell

给这只猫换皮肤的另一种方法是使用 powershell(如果你安装了它)来调用 wmi 调用,包括对 System.Management.Automation 的引用,它最有可能在 \program files\reference assembly\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...
}