This may get you started Mike
.. uses WNetGetConnection
You'll just need to strip the Drive from your Qualified Path
and test the Drive with WNetGetConnection
and add the trailing Path to the ServerName returned.
using System;
using System.IO; // Directory
using System.Text; // StringBuilder
using System.Runtime.InteropServices; // DllImport
/*
References :
System.dll
*/
//----------------------------------------------
// kwb: 2007/11/17 13:41:02
//----------------------------------------------
namespace GetUNCPath
{
class Program
{
#region API functions / calls
//======================================================================================
// Use DllImport to import the Win32 MessageBox function.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(
IntPtr hWnd,
String text,
String caption,
uint type);
[DllImport("kernel32.dll")]
static extern uint QueryDosDevice(
string lpDeviceName,
StringBuilder lpTargetPath,
int ucchMax );
[DllImport("mpr.dll", EntryPoint = "WNetGetConnection", SetLastError = true)]
public static extern int WNetGetConnection(
string strLocalName,
StringBuilder strbldRemoteName,
ref int intRemoteNameLength); // <--- Note ref
//======================================================================================
#endregion
static void Main(string[] args)
{
try
{
Console.WriteLine("CurrentDirectory: {0}", Environment.CurrentDirectory);
Console.WriteLine("UserName: {0}", Environment.UserName);
Console.WriteLine("MachineName: {0}", Environment.MachineName);
Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);
//
string[] drives = Directory.GetLogicalDrives();
Console.WriteLine("Directory.GetLogicalDrives(): {0}", String.Join(", ", drives));
//
for (int i = 0; i < drives.Length; i++)
Console.WriteLine("RealPath :{0} -> {1}",
(drives[i]),
(GetRealPath(drives[i])));
//------------------------------------------------------------------------------------
StringBuilder strbldRemoteName = new StringBuilder();
int intRemoteNameLength = 100; // <--- Note Set the max. number of char
strbldRemoteName.Capacity = intRemoteNameLength;
Console.Write(@"Enter Drive: ");
string strDrive = Console.ReadLine();
// status = 0 (No Error)
int status = WNetGetConnection(
strDrive,
strbldRemoteName,
ref intRemoteNameLength);
Console.WriteLine("UNC Path :{0} -> {1}", strDrive, strbldRemoteName.ToString());
MessageBox(new IntPtr(0), strbldRemoteName.ToString(), "TheSwampRules OK", 0);
Console.Read();
}
catch (IOException e) { Console.WriteLine(e.ToString()); }
}
//======================================================================================
//
// Modified code from http://www.pinvoke.net/default.aspx/kernel32.QueryDosDevice
//
private static string GetRealPath(string path)
{
string realPath;
StringBuilder pathInformation = new StringBuilder(250);
// Get the drive letter of the
string driveLetter = Path.GetPathRoot(path).Replace("\\", "");
QueryDosDevice(driveLetter, pathInformation, 250);
// If drive is substed, the result will be in the format of "\??\C:\RealPath\".
if (pathInformation.ToString().Contains("\\??\\"))
{
// Strip the \??\ prefix.
string realRoot = pathInformation.ToString().Remove(0, 4);
//Combine the paths.
realPath = Path.Combine(realRoot, path.Replace(Path.GetPathRoot(path), ""));
}
else
{
realPath = path;
}
return realPath;
}
//======================================================================================
//======================================================================================
//======================================================================================
}
}
And the obligatory piccy ..