mwalts

Hello again all,

I might be using the wrong terminology so I'll give a quick example. I want to find the \\machinename\ShareName if one exists for a given local directory.
i.e. if given C:\Folder on machine PK-234, and that folder is shared with a name of Folder, I would like to get \\PK-234\Folder

Any help is much appreciated.

Thanks,

-mwalts


Re: Visual C# General How to find Network path from local path?

OmegaMan

Your questions are confusing, you mention local then remote. Do you want to know if a remote directory is accessible or a local one is currently being shared





Re: Visual C# General How to find Network path from local path?

mwalts

Ok, sorry about that.

I want to know both if a local folder is being shared, and what name it is being shared under.

I'll attempt to explain what I'm doing here.

I have remoting program, and one of the remote objects holds some setting information, including paths to certain network folders that the client may have to place files in. Normally these network folders will reside in the same place as the server.

I have created a form that allows those with physical access to the server to alter these settings without having to alter the XML settings file the server uses by hand. This form includes some FolderBrowserDialogs(FBDs). If the user were to select the local path to the folder through the FBDs it will cause a problem as the client(s) will be unable to access the folder under that path. I would like to convert this local name to the name the folder is being shared under (or if it isn't being shared warn the user and don't change the setting)

Hope that clears it up, and thanks for the quick reply,

-mwalts




Re: Visual C# General How to find Network path from local path?

Peter Ritchie

How about:

Code Snippet

///
/// Gets the local path, if applicable, of a UNC
///
///
/// Path or String.Empty if not a local share.
/// Created.
///
///
public static bool TryGetLocalUncDirectory(String unc, out DirectoryInfo directoryInfo)
{
if(String.IsNullOrEmpty(unc))
{
throw new ArgumentNullException("unc");
}
unc = unc.Replace(@"\\", "");
String[] parts = unc.Split('\\');
if (parts.Length < 2)
{
throw new ArgumentException("unc");
}

String computer = parts[0];
parts.SetValue("", 0);
String name = String.Join(@"\", parts).Substring(1);

ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Path FROM Win32_share WHERE Name = '" + name + "'");
ManagementObjectCollection coll = searcher.Get();
if (coll.Count == 1)
{
foreach (ManagementObject share in searcher.Get())
{
directoryInfo = new DirectoryInfo(share["Path"] as String);
return true;
}
}
directoryInfo = null;
return false;
}

///
/// The the local path, if applicable, of a shared drive.
///
///
///
///
/// created
public static bool TryGetLocalShareDirectory(DriveInfo drive, out DirectoryInfo path)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + drive.Name.Substring(0, 2) + "'");
foreach (ManagementObject managementObject in searcher.Get())
{
TryGetLocalUncDirectory(managementObject["RemoteName"] as String, out path);
return true;
}
path = null;
return false;
}

Code Snippet

//...
DirectoryInfo directoryInfo;
TryGetLocalShareDirectory(new DriveInfo("X:"), out directoryInfo);






Re: Visual C# General How to find Network path from local path?

mwalts

Thank you,

I'm using .NET 1.1 so I had to change things around a bit. The network is down today, so I can't really test this properly (always returns 0 results, which I guess is no surprise).

Anyway, looks promising, I'll get back to you on how it turns out

-mwalts





Re: Visual C# General How to find Network path from local path?

Peter Ritchie

mwalts wrote:

Thank you,

I'm using .NET 1.1 so I had to change things around a bit. The network is down today, so I can't really test this properly (always returns 0 results, which I guess is no surprise).

Anyway, looks promising, I'll get back to you on how it turns out

-mwalts

You should have mentioned that, I could have made sure it works in 1.1 as well. Let me know if you need help with it.




Re: Visual C# General How to find Network path from local path?

mwalts

Sorry, anyway, the network came up about 20 minutes ago, so here is what I ended up doing for those interested. As you can see, the sample provided originally worked in the opposite direction.

Code Snippet

/// <summary>
/// Gets the UNC path for a local path passed to it. empty string if folder isn't shared
///
/// </summary>
/// <param name="local">The local path</param>
/// <param name="unc">The UNC path</param>
/// <returns>True if UNC is valid, false otherwise</returns>
public static bool TryGetLocalFromUncDirectory(string local, out string unc)
{
if((local == null) || (local==""))
{
unc = "";
throw new ArgumentNullException("local");
}


ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Name FROM Win32_share WHERE path ='" + local.Replace("\\","\\\\") + "'");
ManagementObjectCollection coll = searcher.Get();
if (coll.Count == 1)
{
foreach (ManagementObject share in searcher.Get())
{
unc = share["Name"] as String;
unc = "\\\\" + SystemInformation.ComputerName + "\\" + unc;
return true;
}
}
unc = "";
return false;
}


Called as such

string path = "C:\sharedFolder"
string unc;
try{
if(TryGetLocalFromUncDirectory(path,out unc))
{
//do stuff
}
}catch(ArgumentNullException ex)
{
Console.WriteLine("No path specified");
}

Thanks again Peter, that sample really helped me out
-mwalts