Click here to Skip to main content
13,145,851 members (52,654 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 20 Feb 2011

Convert a file path to a UNC Path

, 20 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Here is an API way using WNetGetUniversalNameImports System.Runtime.InteropServices _ Private Shared Function GetUName(ByVal Path As String, ByVal outName As Integer, ByVal bObj As...
Here is an API way using WNetGetUniversalName

Imports System.Runtime.InteropServices
  <DllImport("mpr.dll", Entrypoint:="WNetGetUniversalName", CharSet:=CharSet.Auto, SetLastError:=False)> _
  Private Shared Function GetUName(ByVal Path As String, ByVal outName As Integer, ByVal bObj As IntPtr, ByRef bSize As Integer) As Integer
  End Function
  <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto), Serializable()> Class UNIVERSAL_NAME_INFO
    <MarshalAs(UnmanagedType.LPTStr)> Public UniversalName As String
    <MarshalAs(UnmanagedType.LPTStr)> Public ConnectionName As String
    <MarshalAs(UnmanagedType.LPTStr)> Public RemainingPath As String
  End Class
  Dim size As Integer = 1024
  Dim buffer As IntPtr = Marshal.AllocHGlobal(size)
  Public Function getUNC(ByVal path As String)
    Dim uncpath As String = path
    Dim ret As Int32 = GetUName(path, 1, buffer, size)
    If ret = 0 Then
      Marshal.PtrToStructure(buffer, unc)
      uncpath = unc.UniversalName
    End If
    Return uncpath
  End Function


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Prerak Patel
Software Developer
India India
Working since 2006 on VBA, VB6, VB.Net, C#, ASP.Net, MSSQL

  • Courage is not the absence of fear, but rather the judgement that something is more important than fear.

  • The fear of suffering is worse than the suffering itself.

  • People need not fear the unknown if they are capable of achieving what they need and want.

  • Every blessing ignored becomes a curse.

  • Sometimes what's in your head isn't as crazy as you think.

  • We never really grow up, we only learn how to act in public.

  • You can make very bad teams with very good individuals.

  • Admitting mistakes means you have a sense of responsibility in your actions and that shows you are more matured than almost anyone. -Nithin

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 21 Feb 2011
Article Copyright 2011 by Prerak Patel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid