Consider the following UNC path to a simple windows network share (SMB protocol):
\\host\share
You can easily create an instance of System.Uri
or System.IO.DirectoryInfo
by passing that path as a string to the constructor.
Now consider the following two paths, leaving all security concerns aside:
\\username@host\share
\\username:password@host\share
You can't simply pass these to the Uri or DirectoryInfo constructors because they don't know how to handle these paths, which are mostly used by linux systems. Besides, even if they did, they most likely wouldn't preserve the logon credentials.
Ultimately I would like a way to convert these to an instance of System.Uri
that represents the actual path, and an instance of System.Net.NetworkCredential
that stores the logon information.
So how do I tackle this problem? I don't even know where to begin. Inheriting from System.UriParser
seems to be a good place to start but according to MSDN, it is not recommended.
Microsoft strongly recommends that you use a parser shipped with the .NET Framework. Building your own parser increases the complexity of your application, and will not perform as well as the shipped parsers.
Thanks.
EDIT
The simple solution (as posted below) is to just assume that everything before the '@' symbol is part of the logon credential, and everything after it is the actual path. This can be problematic however when filenames contain '@' symbols.
\\credential@host\path
: no problem
\\credential@host\path@mystorage
: again no problem because everything before the first @ is part of the logon.
\\host\path@mystorage
: now we're busted. This will definitely throw argument exceptions when passed as an argument.