簡體   English   中英

獲取FileInfo的文件的絕對服務器路徑(物理路徑)

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

我正在使用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" />

這是提供者的定義:

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

這就是我的問題 - 如果我像這樣定義一個字符串:

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

FileInfo file = new FileInfo(path);

FileInfo將無法找到此文件,因為它使用的是virtualPath而不是physicalPath。

我如何獲取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);

再次閱讀問題,正確的方法似乎是VirtualPathUnifiedProvider.TryGetHandledAbsolutePath

有了它,你會做這樣的事情:

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

FileInfo file = new FileInfo(path);

這是你可以做的(如果你只知道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);
    }
}

如果您已經知道文件的完整虛擬路徑,則可以直接轉到VirtualPathHandler.Instance.GetFile(...)。

您需要的命名空間是EPiServer.Web和EPiServer.Web.Hosting(和System.IO)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM