Suppose there is \\share\lala
, how can I programmatically find out which letter of the disk on the server corresponds to this ball?
- in the sense of? you know the address of the balls and want to know from which disk it is distributed or you have the ball connected as a network drive to a remote machine and you need to know the drive letter with which this ball is associated with the remote machine? - rdorn
- @rdorn I have the address of the balls and I want from which disk it is distributed on the server - iluxa1810
1 answer
This can be done through WMI for which in .NET there is a wrapper.
You will need to add a reference to System.Management.DLL
in the project and specify in the code using System.Management;
We will display all the existing "balls" with the full path to the local folder.
var oManager = new ManagementClass("Win32_Share"); foreach(ManagementObject oShare in oManager.GetInstances()) { var strShareName = oShare .Properties .Cast<PropertyData>() .First(x => x.Name == "Name") .Value.ToString(); var strSharePath = oShare .Properties .Cast<PropertyData>() .First(x => x.Name == "Path") .Value.ToString(); if(strSharePath == "") strSharePath = "***UNDEFINED***"; Console.WriteLine(strShareName + " - " + strSharePath); }
strPath
will contain the full path to the ball, from which to get the drive letter is already quite simple - like this:
var di = new DirectoryInfo(strSharePath); Console.WriteLine(di.Root);
By adding quite a bit of code, you can search for the data "balls" by its network name.
The above example allows you to find out the paths to shared folders on a local machine, but WMI also allows you to access remote machines, if you have the appropriate user rights and necessary permissions on a remote machine. Read more about remote connection and WMI security in MSDN documentation here and here.
The idea is taken from pinvoke.net , there are generally many interesting things about using WinAPI under .NET