0

I want to specify a virtual path of a file but I am not able to do so.

Here is my code:

int Course_Id = 1;
int PortfolioID=1;
int ChandidateID=1;

string filepath = Server.MapPath("~/ePortfolio/PortFolioContent/" + Course_Id + "/Assignments/Exhb_" + item.PortfolioID + "_" + item.ChandidateID + ".jpg");

if(System.IO.File.Exists((filepath)))
{                            
    ViewBag.Thumbnail = filepath;
}

When i use Server.MapPath it is fetching data from some other path:
My original path is:
E:\HOBBYHOMES(E-PORTFOLIO)\HobbyHomesWebApp\ePortfolio\PortFolioContent\1\Assignments
but it is taking this path
E:\HOBBYHOMES(E-PORTFOLIO)\HobbyHomesWebApp\ActivityPostin\Images\ePortfolio\PortFolioContent\1\Assignments\Exhb_1_1.jpg
I m not understanding why this is workin so.Please help me

1
  • 1
    I don't have a good solution for you, but here is why what you've tried so far doesn't work. I don't think ~ expands to anything in filepaths on Windows--some command shells do that but not the filesystem itself. '..' is going to be based on the current working directory (which is likely the directory of the executable you are running, or something equally useless). Commented May 23, 2012 at 2:54

3 Answers 3

3

Try string filepath = Server.MapPath("~/... ...jpg");

Sign up to request clarification or add additional context in comments.

1 Comment

hey thanx 4 d reply..i used ur method but still the prob is nt resolved.Please see i hav updated my question.
0

You can use @Url.Content("~/Content/Images/YourImageName.png")

3 Comments

hey thanx 4 d reply but wats the problem with this path..act i am bound to use this path only
Your are going for the following path string filepath = Server.MapPath("~/ePortfolio/e-Portfolio/PortFolioContent/" + Course_Id + "/Assignments/Exhb_" + PortfolioID + "_" + ChandidateID + ".jpg"); 1) E:\HOBBYHOMES(E-PORTFOLIO)\HobbyHomesWebApp\ePortfolio\PortFolioContent\1\Assignments 2) E:\HOBBYHOMES(E-PORTFOLIO)\HobbyHomesWebApp\ActivityPostin\Images\ePortfolio\PortFolioContent\1\Assignments\Exhb_1_1.jpg I can not see folder e-Portfolio. Can you please update your question again and what exact path you want?As per me you want 2nd path and getting first path.
hey sry i had pasted the wrong path forgotto do it while editing..and thanx anyways now it is working:)
0

Try doing something like this:

Path.Combine(Server.MapPath("~/"),("ePortfolio\\PortFolioContent\\" + Course_Id + "\\Assignments\\Exhb_" + item.PortfolioID + "_" + item.ChandidateID + ".jpg"));

In theory it results in the same answer that failed you previously. In practice, it may yield a completely different answer.

There's also a recent question here (which I can't find at the moment) which highlights that sometimes Server.MapPath("~/..etc..") misbehaves on rewritten request paths. Are you rewriting the request that is trying to form this path?

1 Comment

nopes m nt calling it again i wrote it like this string filepath = Server.MapPath("~/ePortfolio/PortFolioContent/" + Course_Id + "/Assignments/Exhb_" + item.PortfolioID + "_" + item.ChandidateID + ".jpg"); earlier i ah nt placed in the folder from where it was executing so that may be problem.so now i got it working:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.