2

I'm searching for a datatype to store UNC paths in. I already tried using Uri but I always get an UriFormatException telling me

Invalid URI: The hostname could not be parsed.

The UNC path I'm using is \\.\Dap0 which represents a Data Acquisition Processor I'm working with.

Does anyone know such a datatype? Maybe one which makes it possible to work with the path? Or did I miss something when using Uri?

Does the path need to be accessible when creating an Uri object? Because currently it isn't, I'm only using this path for testing purposes.

Community
  • 1
  • 1
Pete Hilde
  • 659
  • 1
  • 10
  • 24
  • Probably `string` then. A URI requires a scheme like `http` or `ftp`. – DavidG Jul 20 '18 at 09:19
  • Though a string like this might work: `file://./Dap0` – DavidG Jul 20 '18 at 09:25
  • Oh sorry, I forgot to mention that I already tried using `string` because I actually want to avoid it. I'm currently trying to create a constructor for an exception and If I used `string` as the datatype for the UNC path I would have two constructors with `DAPException(string, string)` as the signature. – Pete Hilde Jul 20 '18 at 09:25
  • `file://./Dap0` doesn't work, I still get the same exception. – Pete Hilde Jul 20 '18 at 09:28
  • The problem is the `.` for the hostname, the `Uri` class just doesn't like it at all. – DavidG Jul 20 '18 at 09:30
  • Translate the dot to the hostname in the constructor System.Net.Dns.GetHostName() and then use the Uri.IsUnc to check if it is valid – Ranjith Venkatesh Jul 20 '18 at 09:34

1 Answers1

1

Uri is the right framework class for UNC paths and even has an IsUncPath method to validate this.

The problem here is you're using \\.\ which is not a standard UNC path and is used to access the Win32 device namespace. You can see here for more details.

Note that (from the document linked above):

Most APIs won't support "\.\"; only those that are designed to work with the device namespace will recognize it. Always check the reference topic for each API to be sure.

So in your specific situation you are probably going to have work with a string or roll your own class.

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64