Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if a folder is shared in .NET

Tags:

c#

.net

windows

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.

like image 533
Dana Avatar asked Sep 25 '08 22:09

Dana


People also ask

How do you check if a folder is shared or not?

To find and access a shared folder or printer: Search for Network , and click to open it. Select Search Active Directory at the top of the window; you may need to first select the Network tab on the upper left. From the drop-down menu next to "Find:", select either Printers or Shared Folders.

How do I find shared folders in Windows server?

Start -> Run -> compmgmt. msc -> OK. Computer Management (Local) -> System Tools -> Shared Folders -> Shares. This will show you all the current shares on the system as well as allow you to control them, change permissions, modify access, etc.

How do I view a shared folder in run?

There are a couple of ways to connect to a folder that has been shared over the network. The fastest way is by typing the Universal Naming Convention (UNC) path in the Start, Run dialog box. The UNC defines the path to a shared folder using the format \\server\sharename.


2 Answers

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

like image 149
JohnIdol Avatar answered Oct 20 '22 19:10

JohnIdol


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.

like image 38
Yuval Peled Avatar answered Oct 20 '22 17:10

Yuval Peled