c#+编码后的服务器虚拟路径,.net c#如何将物理路径转换为虚拟路径

博客讨论了如何将从数据库中获取的绝对路径转换为网站可用的虚拟路径。问题在于绝对路径包含过多重复的文件夹名,需要进行简化。解决方案包括使用Split函数分割路径并构建新的虚拟路径,或者通过替换物理路径来获取相对路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Needs to be changed to:

~/image/Vince/Tulips.jpg

Talk1:

Not clear, what does the first path means? Is it where your Website is hosted?

Talk3:

The first path is the root path where I saved it from the database. Then I need to get the path from Database to change the image but it cannot be change.

Talk4:

If I understand you correctly, you want to create a virtual directory that points to image folder.

Talk5:

I think the problem is that you don't have enough repetitions of AlumniTrackingSystem in your path. I think another 2 should do the trick.

Solutions1

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];

}

Solutions2

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\

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值