【问题标题】:Get the absolute server path(physical path) of file for FileInfo获取 FileInfo 文件的服务器绝对路径(物理路径)
【发布时间】:2012-08-10 11:40:17
【问题描述】:

我正在使用 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 将无法找到此文件,因为它使用的是虚拟路径而不是物理路径。

如何获取物理路径,以便我能够找到带有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);

【问题讨论】:

  • 您是否尝试过编写System.Path.Combine 方法而不是String.Concat(.., .. );?如果将physicalPath="C:\Temp Files\Workplaces" + string path = "2999/Documents/document.txt"(斜杠替换为反斜杠)结合起来会怎样?
  • @aleksey.berezan - System.IO.Path.Combine,但除此之外是的,值得一提。
  • 使用 Path.Combine 和 Strin.Concat 我获得了相同的“MyProjectName/WorkplaceFiles/2999/Documents/document.txt”。对于 FileInfo 部分,我需要从 C:\Temp Files\... 开始的完整路径

标签: c# path virtualpathprovider episerver-6


【解决方案1】:

再次阅读问题,正确的方法似乎是VirtualPathUnifiedProvider.TryGetHandledAbsolutePath

有了它,你会做这样的事情:

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

FileInfo file = new FileInfo(path);

【讨论】:

  • 我真的不明白这对我有什么帮助。
  • 我有点认为将虚拟路径映射到物理路径的方法显然会有所帮助。但是再次阅读您的问题,这可能是 EpiServer 特定的,以至于我的回答无效。 :(
  • 这个版本会更接近您的需求吗?我的 EpiServer 经验为零……
  • 感谢您的编辑。我不知道如何使用 TryGetHandledAbsolutePath,因为它给我带来了以下错误:EPiServer.Web.Hosting .... TryGetHandledAbsolutePath 由于其保护级别而无法访问。
  • 是的,所以这是一种受保护的方法。傻我。我再看看能不能找到有用的东西。
【解决方案2】:

这是您可以做的(如果您只知道 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)。

【讨论】:

    猜你喜欢
    • 2011-09-20
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 2021-10-18
    • 2010-09-18
    相关资源
    最近更新 更多