none
Extract computer name from unc path RRS feed

  • Question

  • I'm trying to figure out how to just return the computer name from eg

    "\\DESKTOP-1\DIRECTORY1\DIRECTORY2\......\DIRECTORYn\FILE.EXT"

    ie I just want to return 

    "\\DESKTOP-1"

    Do I need a regular expression for this?

    Thanks

    Andy

    Saturday, May 18, 2019 8:42 AM

All replies

  • Here is something to try without regular expressions assuming the shown is the basic format.

    Dim pathValue = "\\DESKTOP-1\DIRECTORY1\DIRECTORY2\......\DIRECTORYn\FILE.EXT"
    Dim value = "\\" & pathValue.Split("\"c).FirstOrDefault(Function(element) Not String.IsNullOrWhiteSpace(element))


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, May 18, 2019 4:14 PM
    Moderator
  • You can do :

    Dim uriPath As Uri = New Uri("\\DESKTOP-1\DIRECTORY1\DIRECTORY2\......\DIRECTORYn\FILE.EXT")
    Console.WriteLine(uriPath.Host)

    Saturday, May 18, 2019 4:24 PM