Hi,
I want to convert local or mapped path into UNC path.
I was able to get the code for mapped path but same code is not working for local drive.
Please provide me solution asap.
Curently i have following code:
[DllImport("mpr.dll")]
private static extern int WNetGetUniversalName (string lpLocalPath,
int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize);
[DllImport("mpr", CharSet=CharSet.Auto)]
protected static extern int WNetGetUniversalName (string lpLocalPath,
int dwInfoLevel, IntPtr lpBuffer, ref int lpBufferSize);
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct UNIVERSAL_NAME_INFO
{
[MarshalAs(UnmanagedType.LPTStr)]
public string lpUniversalName;
}
//
//Constants
//
protected const int NO_ERROR = 0;
protected const int ERROR_MORE_DATA = 234;
protected const int ERROR_NOT_CONNECTED = 2250;
protected const int UNIVERSAL_NAME_INFO_LEVEL = 1;
//
//GetUNCPath function
//
public string GetUNCPath(string mappedDrive)
{
UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
int bufferSize = Marshal.SizeOf(rni);
int nRet = WNetGetUniversalName(
mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,
ref rni, ref bufferSize);
if (ERROR_MORE_DATA == nRet)
{
IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);;
try
{
nRet = WNetGetUniversalName(
mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,
pBuffer, ref bufferSize);
if (NO_ERROR == nRet)
{
rni = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffer,typeof(UNIVERSAL_NAME_INFO));
}
}
finally
{
Marshal.FreeHGlobal(pBuffer);
}
}
switch (nRet)
{
case NO_ERROR:
return rni.lpUniversalName;
case ERROR_NOT_CONNECTED:
//Local file-name
MessageBox.Show("Share not connected");
return string.Empty;
default:
return string.Empty;
}
return string.Empty;
}
result:
Y: == \\remotemachine\folder1
C: == network not connected
Expected:
Y: == \\remotemachine\folder1
C: == \\MyMachineName\c$\
I want to convert local or mapped path into UNC path.
I was able to get the code for mapped path but same code is not working for local drive.
Please provide me solution asap.
Curently i have following code:
[DllImport("mpr.dll")]
private static extern int WNetGetUniversalName (string lpLocalPath,
int dwInfoLevel, ref UNIVERSAL_NAME_INFO lpBuffer, ref int lpBufferSize);
[DllImport("mpr", CharSet=CharSet.Auto)]
protected static extern int WNetGetUniversalName (string lpLocalPath,
int dwInfoLevel, IntPtr lpBuffer, ref int lpBufferSize);
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct UNIVERSAL_NAME_INFO
{
[MarshalAs(UnmanagedType.LPTStr)]
public string lpUniversalName;
}
//
//Constants
//
protected const int NO_ERROR = 0;
protected const int ERROR_MORE_DATA = 234;
protected const int ERROR_NOT_CONNECTED = 2250;
protected const int UNIVERSAL_NAME_INFO_LEVEL = 1;
//
//GetUNCPath function
//
public string GetUNCPath(string mappedDrive)
{
UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
int bufferSize = Marshal.SizeOf(rni);
int nRet = WNetGetUniversalName(
mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,
ref rni, ref bufferSize);
if (ERROR_MORE_DATA == nRet)
{
IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);;
try
{
nRet = WNetGetUniversalName(
mappedDrive, UNIVERSAL_NAME_INFO_LEVEL,
pBuffer, ref bufferSize);
if (NO_ERROR == nRet)
{
rni = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffer,typeof(UNIVERSAL_NAME_INFO));
}
}
finally
{
Marshal.FreeHGlobal(pBuffer);
}
}
switch (nRet)
{
case NO_ERROR:
return rni.lpUniversalName;
case ERROR_NOT_CONNECTED:
//Local file-name
MessageBox.Show("Share not connected");
return string.Empty;
default:
return string.Empty;
}
return string.Empty;
}
result:
Y: == \\remotemachine\folder1
C: == network not connected
Expected:
Y: == \\remotemachine\folder1
C: == \\MyMachineName\c$\