简体   繁体   English

如何将物理路径转换为虚拟路径

[英]How to convert physical path to virtual path

Below you can see the path that has been stored in the database and what i need it to look like. 在下面,您可以看到已存储在数据库中的路径以及所需的外观。 So I can use the path to change an image. 因此,我可以使用该路径来更改图像。

From the Database: 从数据库中:

C:\\Users\\AlphaDog\\Desktop\\Alumni Revised\\AlumiTrackingSystem\\AlumiTrackingSystem\\AlumiTrackingSystem\\AlumiTrackingSystem\\image\\Vince\\Tulips.jpg C:\\ Users \\ AlphaDog \\ Desktop \\ Alumni Revised \\ AlumiTrackingSystem \\ AlumiTrackingSystem \\ AlumiTrackingSystem \\ AlumiTrackingSystem \\ image \\ Vince \\ Tulips.jpg

Needs to be changed to: 需要更改为:

~/image/Vince/Tulips.jpg

i hope this is what you are looking for: 我希望这是您要寻找的:

String RelativePath = AbsolutePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);

it should work if your app phisical path is: 如果您的应用物理路径为:

C:\Users\AlphaDog\Desktop\Alumni Revised\AlumiTrackingSystem\AlumiTrackingSystem\AlumiTrackingSystem\AlumiTrackingSystem\

Something like the following should do the trick. 像下面这样的东西应该可以解决问题。 A little more code than the previous answer, but you know, sometimes I like to do things the hard way. 比先前的答案要多一些代码,但是您知道,有时候我喜欢用困难的方式做事。

    string path = @"C:\Users\AlphaDog\Desktop\Alumni Revised\AlumiTrackingSystem\AlumiTrackingSystem\AlumiTrackingSystem\AlumiTrackingSystem\image\Vince\Tulips.jpg";
    string[] splitPath = path.Split('\\');
    int start = 0;
    foreach (string s in splitPath) {
         if (s == "image")
             break;
         else
             start++;
    }
    string virtualPath = "~/";
    for (int i = start; start < splitPath.Length; start++) {
         virtualPath += (i > start ? "/" : "") + splitPath[start];
    }

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

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