Get the absolute server path(physical path) of file for FileInfo

4.8k views Asked by At

I'm using an EPi server provider:

<add virtualPath="~/WorkplaceFiles/" physicalPath="C:\Temp Files\Workplaces"
                  name="workplaceFiles" type="EPiServer.Web.Hosting.VirtualPathNativeProvider,EPiServer"
                  showInFileManager="true" virtualName="workplaceUploadDocuments"  bypassAccessCheck="true" maxVersions="5" />

Here's the defination for the provider:

VirtualPathUnifiedProvider provider =
    VirtualPathHandler.GetProvider(DocumentConstants.WorkplaceFiles) as VirtualPathUnifiedProvider;

And here comes my problem - if I define a string for example like this:

string path = "2999/Documents/document.txt"
path = String.Concat(provider.VirtualPathRoot, path);

FileInfo file = new FileInfo(path);

FileInfo won't be able to find this file, because it's using the virtualPath not the physicalPath.

How can I take the physicalPath, so that I will be able to find the file with FileInfo?

// When I'm on  this line I would like my path string to be "C:\Temp Files\Workplaces\2999\Documents\document.txt"
FileInfo file = new FileInfo(path);
2

There are 2 answers

5
Rytmis On

Reading the question again, the proper method seems to be VirtualPathUnifiedProvider.TryGetHandledAbsolutePath

With it, you'd do something like this:

string path;
provider.TryGetHandledAbsolutePath("2999/Documents/document.txt", out path);

FileInfo file = new FileInfo(path);
0
PaddySe On

This is what you could do (if you only know the name of the VPP provider):

const string path = "Testbilder/startsidan_896x240.jpg";
var provider = VirtualPathHandler.GetProvider("SiteGlobalFiles") as VirtualPathUnifiedProvider;
if (provider != null)
{
    var virtualPath = VirtualPathUtilityEx.Combine(provider.VirtualPathRoot, path);
    var file = VirtualPathHandler.Instance.GetFile(virtualPath, true) as UnifiedFile;
    if (file != null)
    {
        var fileInfo = new FileInfo(file.LocalPath);
    }
}

If you already know the full virtual path of the file, you can go directly to VirtualPathHandler.Instance.GetFile(...).

The namespaces you need are EPiServer.Web and EPiServer.Web.Hosting (and System.IO).