1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace WindowsFormsApplication1
{
class Network
{
[DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool PathIsNetworkPath([MarshalAs(UnmanagedType.LPTStr)]string pszPath);
[DllImport("mpr.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "WNetGetConnectionW")]
public static extern int WNetGetConnection([MarshalAs(UnmanagedType.LPWStr)]string lpLocalName, StringBuilder lpRemoteName, ref int lpnLength);
static public string GetUNCPath(string NetworkPath)
{
string szRoot = NetworkPath.Substring(0, 2);
string szPath = NetworkPath.Replace(@"\", @"\\");
if (PathIsNetworkPath(szPath))
{
StringBuilder szbUNC = new StringBuilder(260);
int iLength = 260;
if (WNetGetConnection(szRoot, szbUNC, ref iLength) == 0)
{
szPath = Path.Combine(szbUNC.ToString(), Path.GetFileName(szPath));
}
}
return szPath;
}
}
} |