Reputation: 2149
I want to convert "C:\Program Files\x\y\z"
to "C:\%ProgramFiles%\x\y\z
OR "C:\Progra~1\x\y\z"
(DOS short path). How to do this C#.Net?
To convert long path to DOS short path I have tried solution posted here however on my Windows 10 it is not working. Any hints?
Update - Please note, path may or may not exist physically on disk. I am looking a solution which can convert any arbitrary path string to above formats. The solution using GetShortPathName(...) works only if the LongPath is physically exist on disk.
Upvotes: 0
Views: 603
Reputation: 74605
If you have a string put into your app such as "c:\program files\x\y\z" and you know it's the wrong place, but don't know where the right place is, you're going to have to make the assumption that c:\program files
means "the program files folder" and perform a string replacement with the result of Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
or Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
depending on which you assert it to be
var wrongPath = @"c:\program files\x\y\z";
var actualPath = wrongpath.Replace(
@"c:\program files",
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
);
For conversion to a short path, try something like: Method to convert a long path name to a short path returns a null
Upvotes: 2