Michael Tot Korsgaard
Michael Tot Korsgaard

Reputation: 4014

ASP.NET - Navigate relative to folder

"Long time no see ASP.NET" and a lot has been forgotten. When I try to look for the folder images I can't seem to find it, unless I use an absolute path, which is not preferred, how would I do this?

My folder structure is like this

+ C
+--+ project
   +--+ controls
      +--+ slideshow
         +--- slideshow.ascx
         +--+ images
            +--- 1.png
            +--- 2.png

In my slideshow.ascx.cs file I've got this:

       string imagePath = @"C:\project\controls\slideshow\images";

            if (Directory.Exists(imagePath))
            {
                try
                {
                    string[] files = Directory.GetFiles(imagePath);
                    List<CarParts24.Source._2ModelEntity.Image> images = new List<Source._2ModelEntity.Image>();

                    foreach (string image in files)
                    {
                        images.Add(new CarParts24.Source._2ModelEntity.Image(image));
                    }

                    litSlideshow.Text = "<span id=\"slideshow-urls\">" + JsonConvert.SerializeObject(images) + "</span>";
                }
                catch (Exception ex)
                {
                    Console.WriteLine(new DateTime() + " - ERROR: " + ex);
                }
            }

which works, but how would I change imagePath into a relative path?

Upvotes: 1

Views: 268

Answers (1)

Clint B
Clint B

Reputation: 4700

Assuming your web site is pointed at C:\project.

Server.MapPath("~") + @"\controls\slideshow\images"

Upvotes: 1

Related Questions