1

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
gunr2171
  • 16,104
  • 25
  • 61
  • 88
User072912
  • 55
  • 1
  • 6
  • Not clear, what does the first path means? Is it where your Website is hosted? – Dalorzo Aug 28 '14 at 17:49
  • 2
    possible duplicate of [Getting relative virtual path from physical path](http://stackoverflow.com/questions/6081433/getting-relative-virtual-path-from-physical-path) – gunr2171 Aug 28 '14 at 17:50
  • 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. – User072912 Aug 28 '14 at 17:52
  • @gunr2171 not a duplicate man – User072912 Aug 28 '14 at 17:53
  • @User072912 Convince me it's not and I'll remove my close vote – gunr2171 Aug 28 '14 at 17:54
  • 2
    If I understand you correctly, you want to create a virtual directory that points to image folder. – Sanjay Sahani Aug 28 '14 at 17:55
  • @User072912 please walk me trough your issue. You ran a query over a table in a database and it brought back one row with one field with the result being your first string. What do you want to do with it? Where that path is pointing to? Your WebApp or some other folder? – Dalorzo Aug 28 '14 at 17:55
  • @SanjaySahani I want to use the saved path in my database to change the image in my website form. But using the path that comes from my database it cannot be replaced. – User072912 Aug 28 '14 at 17:58
  • @Dalorzo That path pointing to the path where my WebApp project located. The "Vince" there is the user's image folder.. – User072912 Aug 28 '14 at 17:59
  • @User072912 this is not clear. Sorry you are explaining your issue correctly. – Dalorzo Aug 28 '14 at 18:02
  • @Dalorzo I uploaded an image in my WebApp project. Also, I have an image folder in my WebApp. Then in my project I will upload an image then the root path of the uploaded image is saved in the database which is showed above.. I want to use that path to my Image in WebApp project like eg.. Image1.ImageUrl = "~/~/image/Vince/Tulips.jpg" – User072912 Aug 28 '14 at 18:06
  • 2
    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. – Ben Robinson Aug 28 '14 at 18:12
  • @BenRobinson, you forgot a closing tag in your comment. – gunr2171 Aug 28 '14 at 18:14

2 Answers2

0

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\
Diego
  • 17
  • 1
0

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];
    }
phishfordead
  • 377
  • 2
  • 7