UNC. How do I find shared directories?
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
BradPosted Feb 19, 2006, 11:12 PM
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.
BradPosted Feb 13, 2006, 1:41 AM
BradPosted Feb 13, 2006, 12:25 AM
And none of them return for me the shares I have that to other machines..
I'm still looking for a solution for this
BradPosted Feb 12, 2006, 10:54 PM
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
BradPosted Feb 12, 2006, 10:27 PM
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.
AnttiPosted Feb 12, 2006, 6:05 PM
>>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.
BradPosted Feb 12, 2006, 5:29 PM