简体   繁体   English

获取FileInfo的文件的绝对服务器路径(物理路径)

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

I'm using an EPi server provider: 我正在使用EPi服务器提供商:

<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. FileInfo将无法找到此文件,因为它使用的是virtualPath而不是physicalPath。

How can I take the physicalPath, so that I will be able to find the file with FileInfo ? 我如何获取physicalPath,以便我能够使用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);

Reading the question again, the proper method seems to be VirtualPathUnifiedProvider.TryGetHandledAbsolutePath 再次阅读问题,正确的方法似乎是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);

This is what you could do (if you only know the name of the VPP provider): 这是你可以做的(如果你只知道VPP提供者的名字):

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(...). 如果您已经知道文件的完整虚拟路径,则可以直接转到VirtualPathHandler.Instance.GetFile(...)。

The namespaces you need are EPiServer.Web and EPiServer.Web.Hosting (and System.IO). 您需要的命名空间是EPiServer.Web和EPiServer.Web.Hosting(和System.IO)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM