Determining if a folder is shared in .NET

后端 未结 4 901
情书的邮戳
情书的邮戳 2020-11-30 12:42

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

Neither Diretory, DirectoryInfo or FileAttributes seem to have any corresponding

相关标签:
4条回答
  • 2020-11-30 13:08

    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

    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...
    }
    
    0 讨论(0)
  • 2020-11-30 13:10

    You can use WMI Win32_Share. Take a look at:

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

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

    0 讨论(0)
  • 2020-11-30 13:10

    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:

    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 link on bytes

    0 讨论(0)
  • 2020-11-30 13:23

    Try using WMI and doing a SELECT * FROM Win32_ShareToDirectory query.

    0 讨论(0)
提交回复
热议问题