WNetGetConnection - Resolve a UNC Path

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace UncConverter
{
    public static class FileInfo
    {
        [DllImport("mpr.dll")]
        static extern uint WNetGetConnection(string lpLocalName, StringBuilder lpRemoteName, ref int lpnLength);

        public static string ConvertToUnc(string fileName)
        {
            bool isLocal = true;  // assume local until disproved

            // strip trailing backslashes from fileName
            var driveName = fileName.Substring(0, 2);

            int length = 256; // to be on safe side 
            StringBuilder networkShare = new StringBuilder(length);
            uint status = WNetGetConnection(driveName, networkShare, ref length);

            // does a network share exist for this drive?
            if (networkShare.Length != 0)
            {
                // now networkShare contains a UNC path in format \\MachineName\ShareName
                // retrieve the MachineName portion
                String shareName = networkShare.ToString();
                string[] splitShares = shareName.Split('\\');
                // the 3rd array element now contains the machine name
                if (Environment.MachineName != splitShares[2])
                    fileName = shareName + fileName.Substring(2);
            }
            return fileName;
        }
    
    }
}