I want to convert a virtual file path to a physical file path in a windows service.
I know what the physical path is for the virtual directory, so I have the following function that works, but feels like a fudge:
public static string GetPhysicalPathFromVirtual(string rootPath, string virtualPath)
{
int trailingSlash = virtualPath.IndexOf('/', 1) + 1;
int length = virtualPath.Length - trailingSlash;
string stripped = virtualPath.Substring(trailingSlash, length);
stripped = stripped.Replace(@"/", @"\");
return Path.Combine(rootPath, stripped);
}
The following example:
string test = FileHelper.GetPhysicalPathFromVirtual(@"T:\generateddocuments\output\", @"/virtualroot/folder/myfile.pdf");
Returns: T:\generateddocuments\output\folder\myfile.pdf
Is there a more elegant way to do this?