Is there a way to do get local path from UNC path?
For eg: \\server7\hello.jpg should give me D:\attachments\hello.jpg
I am trying to save attachments to the UNC path after applying the windows file name and full path length restrictions . Here I am applying the restrictions by taking UNC path length as reference. But local path length is longer than UNC path and i think because of this I am getting the below exception.
System.IO.PathTooLongException occurred HResult=-2147024690
Message=The specified path, file name, or both are too long. The fully
qualified file name must be less than 260 characters, and the
directory name must be less than 248 characters. Source=mscorlib
StackTrace:
at System.IO.PathHelper.GetFullPathName()
at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share,
Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs,
String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean
checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions
options, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode)
at Presensoft.JournalEmailVerification.EmailVerification.DownloadFailedAttachments(EmailMessage
msg, JournalEmail journalEmail) in
D:\Source\ProductionReleases\Release_8.0.7.0\Email
Archiving\Presensoft.JournalEmailVerification\EmailVerification.cs:line
630 InnerException:
Best Answer
Take a look at this blog article: Get local path from UNC path
The code from article. This function will take a UNC path (for example \server\share or \server\c$\folder and return the local path (for example c:\share or c:\folder).
The function uses the ManagementObjectSearcher to search for shares on the network server. If you do not have read access to this server, you will need to log in using different credentials. Replace the line with the ManagementScope with the following lines: