Help with resolving path & URL issues

I'm trying to test to see if the code works (and obviously it doesn't)

The plan is to have a look at whatever files are in so-and-so folder and turn their names into an array of string arguments that I can tape onto whatever directory the website is in.

problem is, unless I hard-code the directory and TELL it where to look, it gets it wrong every time. The error message is telling me (if I'm reading it right) that it's looking for the folder in c:/Program Files/Visual Studio/etc/etc/etc but the website is in c:/Documents and settings/etc/etc/etc. How to I tell it to grab the path that the website is working out of?

* I have tried using "~/Photos" (error msg above)
* I have tried Page.ResolveUrl
* I have tried some other thing I can't even remember in Request.Something that only threw an HTTP Exception at me

Somebody PLEASE HELP!!!

BTW this is the most recent failed attempt:

protected void Page_Load(object sender, EventArgs e)
Default2 thisPage = new Default2();
FileInfo[] picNames = thisPage.makeArray("~/Photos/Portrait");
public FileInfo[] makeArray(string path)
string myPath = Page.ResolveUrl(@path);
DirectoryInfo picsNum = new DirectoryInfo(myPath);
FileInfo[] files = picsNum.GetFiles("*.jpg");
return files;

Ideally this should be re-usable (I might make a new class in the master.aspx.cs called ImageToys or something to that effect)


  • So glad so many people were willing to help.tone(sarcasm);

    Well if anyone else has this problem, I finally found it like a week later. You can get the dircetory via. Request.PhysicalApplicationPath like this:

    string path = Request.PhysicalApplicationPath;

    and then you tape it onto whatever you wanted to tape it onto...

    path += "Photos\";
  • Even better if the Photos directory is underneath the web root you can do: HttpContext.Current.Server.MapPath("/Photos") which resolves to the full physical path. If you leave off the forward slash the MapPath method resolves a relative path to the currently executing page.

    By the way, a better way to create a directory path is to use System.IO.Path.Combine:
    string path = Path.Combine(Request.PhysicalApplicationPath, "Photos");
    In this case it doesn't matter much, but the combine method conveniently handles cases wherein the directory separators may or may not be present so you don't have to worry about them.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!