Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

c# - Determining if a folder is shared in .NET

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 field.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...