I think you will need to use P/Invoke on WNetGetUniversalName. From
this you can get the UNC path like \\machinename\sharename. This code
worked for me:
[DllImport("mpr.dll")]
[return: MarshalAs(UnmanagedType.U4)]
static extern int WNetGetUniversalName(
string lpLocalPath,
[MarshalAs(UnmanagedType.U4)] int dwInfoLevel,
IntPtr lpBuffer,
[MarshalAs(UnmanagedType.U4)] ref int lpBufferSize);
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode),
Serializable()]
class MyUNC
{
[MarshalAs(UnmanagedType.LPStr)]
public string UniversalName;
[MarshalAs(UnmanagedType.LPStr)]
public string ConnectionName;
[MarshalAs(UnmanagedType.LPStr)]
public string RemainingPath;
}
[STAThread]
static void Main()
{
int bufferSize = 2000;
IntPtr buffer = Marshal.AllocHGlobal(bufferSize);
int ret = WNetGetUniversalName("P:", 2, buffer, ref bufferSize);
if (ret == 0)
{
MyUNC unc = new MyUNC();
Marshal.PtrToStructure(buffer, unc);
Console.WriteLine(unc.UniversalName);
}
Marshal.FreeHGlobal(buffer);
return;
}
John
wh*******@gmail.com wrote:
Hi,
I know that System.Windows.Forms.SystemInformation.ComputerNam e
retrieves the current computer's name, but is it possible to fetch the
computer's name from a file path?
ie. G:\file.txt exists on the computer named Tool, how I can get the
name Tool?
Thanks.
Tommy
--
http://www.treehugger.com