0

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);
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Anton Belev
  • 11,963
  • 22
  • 70
  • 111
  • 1
    Have you tried to write `System.Path.Combine` method instead of `String.Concat(.., .. );`? What if to combine `physicalPath="C:\Temp Files\Workplaces"` + `string path = "2999/Documents/document.txt"`(with slashes replaced by backslashes) ? – alex.b Aug 10 '12 at 12:20
  • @aleksey.berezan - `System.IO.Path.Combine`, but otherwise yes, worth mentioning. – Damien_The_Unbeliever Aug 10 '12 at 12:25
  • With Path.Combine and Strin.Concat I obtain the same "MyProjectName/WorkplaceFiles/2999/Documents/document.txt". And for the FileInfo part I need full path starting from C:\Temp Files\... – Anton Belev Aug 10 '12 at 12:36

2 Answers2

1

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);
Rytmis
  • 31,467
  • 8
  • 60
  • 69
  • I don't really see how this will help me. – Anton Belev Aug 10 '12 at 11:53
  • I kind of thought that a method that maps virtual paths to physical paths would obviously be helpful. But reading your question again, this is probably so EpiServer-specific that my answer isn't valid. :( – Rytmis Aug 10 '12 at 12:15
  • Would this version be closer to what you're looking for? I have zero EpiServer experience... – Rytmis Aug 10 '12 at 12:20
  • Thanks for the Edit. I can't figure how to use TryGetHandledAbsolutePath, because it rises me the following error: EPiServer.Web.Hosting .... TryGetHandledAbsolutePath is inaccessible due to its protection level. – Anton Belev Aug 10 '12 at 12:43
  • Yeah, so it was a protected method. Silly of me. I'll take another look and see if I can figure out something useful. – Rytmis Aug 11 '12 at 15:20
1

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).

PaddySe
  • 468
  • 1
  • 3
  • 11