7
Answers

UNC. How do I find shared directories?

Brad

Brad

19y
2.2k
1
In the application I am currently working on, I need to return a list off the UNC paths to all shared directories.. The same sort of list you get when you open 'My Network Places'. There is a simple solution of looking in the C:\Documents and Settings\\Nethood.. But this is not a good solution. This directory only updates when you manually visit the My Network Places folder. So if a new share becomes available, it will not by found by the application until some user refreshes the My Network Places folder.. I need a proper solution.. Not a clumsy loop though the NetHood folder.. .NET must have functions that can talk to the Network.. That can return for me all of the current network shares.. How do I do that? Where should I start looking for classes and functions to help with this? Thanks EDIT: Spelin mustaks
Answers (7)
0
Brad

Brad

NA 13 0 19y
THE SOLUTION (almost)

I always suspected that this would be just a few lines of code..

Here is the guts of it...

1. Add a reference to: System.DirectoryServices.dll

2. Here is the code...

using System.DirectoryServices;

...

DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach(System.DirectoryServices.DirectoryEntry dDom in root.Children) {
  foreach(System.DirectoryServices.DirectoryEntry dPC in dDom.Children) {
     Console.WriteLine( dDom.Name + "/"+dPC.Name);
  }
}

This returns for me every computer on the workgroup.

Note that if your machine 'knows' of other workgroups, this app might lock up as it looks for computers on workgroup that you are not connected to.

Therefore, put a condition in so it only does the inner loop for machine names, if the workgroup matches what you are looking for. In this case "MYWORKGROUP";

DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach(System.DirectoryServices.DirectoryEntry dDom in root.Children) {
  if (dDom.Name == "MYWORKGROUP"){
    foreach(System.DirectoryServices.DirectoryEntry dPC in dDom.Children) {
      Console.WriteLine( dDom.Name + "/"+dPC.Name);
    }
  }
}

There might be a better way to stop it locking up on a workgroup that the machine has been on before, but is not connected to now, but I don't know it.

Post it here if you work it out.

Anyway.. This is almost the solution. As you can see, I get the workgroup name, and the machine name.. But no folder name. This might no matter in my case, as I know what folder I am looking for. But it would be good to know if that is possible.

0
Brad

Brad

NA 13 0 19y
Or could it be that the ASP.NET user account does not have permission to return a list of network shares (that point to other machines)?
0
Brad

Brad

NA 13 0 19y
I seemed to have found the allowable PropertyData for this object..
IsArrayGets or sets a value indicating whether the property is an array.
IsLocal Gets or sets a value indicating whether the property has been defined in the current WMI class.
Name Gets or sets the name of the property.
Origin Gets or sets the name of the WMI class in the hierarchy in which the property was introduced.
Qualifiers Gets or sets the set of qualifiers defined on the property.
Type Gets or sets the CIM type of the property.
Value Gets or sets the current value of the property.

And none of them return for me the shares I have that to other machines..

I'm still looking for a solution for this

0
Brad

Brad

NA 13 0 19y
I have just found out that the code above, is only returning shares on the local machines. ie: the machine the code is running on..

This machine has many other shared directorys to different machines..

It is them I need a list of.

So instead of this...

\\LocalMachine\Apps
\\LocalMachine\Dev

I need this

\\OtherMachine1\Temp
\\OtherMachine1\Docs
\\AnotherMachine\Images
\\YetAnotherPC\MoreImages
0
Brad

Brad

NA 13 0 19y
Thanks for your reply.. I too looked on Google, but did not find anything as useful as the link you sent.

I have tried it out and it works, mostly..

I have repeated the code here to save others going to that link..

ManagementClass shares = new ManagementClass("Win32_Share" );
try{
ManagementObjectCollection specificShares = exportedShares.GetInstances();
foreach(ManagementObject share in specificShares) {
string sharedFolder = share["Name"];
}
}finally{
shares.dispose();
}

First up, this code has some errors..

The line
ManagementObjectCollection specificShares = exportedShares.GetInstances();
Should be
ManagementObjectCollection specificShares = shares.GetInstances();

Next, this line
string sharedFolder = share["Name"];
should be
string sharedFolder = share["Name"].ToString();

Other than that, make sure you add System.Management to your 'References' and finally, put...
Using System.Management;
At the top of the code.

Now, when I run this code, it returns the shared directory names, perfectly. But it does not return the machine name.. So, instead of getting..

\\Machine1\Apps

.. it gets...

Apps

That line that says 'string sharedFolder = share["Name"]; ' is the key. What other values can I put in share["Name"];?

share["Machine"] maybe... Nope
share["ID"].. nope

There must be a list of these somewhere.

The solution is close.

Thanks again for the link.
0
Antti

Antti

NA 202 0 19y

>>I take it that this is not possible in C# .NET?

Well, this forum seems to be quiet one... (I'm I the only one answering these questions??)
I found this in 30 seconds with google: http://forums.microsoft.com/ MSDN/ShowPost.aspx?PostID=205324&SiteID=1, hope it was the answer you looked for.

0
Brad

Brad

NA 13 0 19y
I take it that this is not possible in C# .NET? Anyone have an idea about this? I am still looking for a solution. Thanks