Solved

Server.MapPath and new folders

Posted on 2009-04-07
8
495 Views
Last Modified: 2012-05-06
I'm using Server.Mappath to return some images and display them in a datalist. The file that stores the images was going to just be the Images folder in my solition. Now before I create and store the images, I'm creating a new folder under the images folder. Everything works except when it goes to display the images it no longer knows where to look. Any idea how to fix this?

dlImages.DataSource = System.IO.Directory.GetFiles(Server.MapPath("~/Images/"));
dlImages.DataBind();

The new folder is created in the code preceding this, so there is a variable I can work with. How can I get it to look for the newly created folder, which will change all of the time based on what was just created?
This did not work:
dlImages.DataSource = System.IO.Directory.GetFiles(Server.MapPath("~/Images/" + newFolder));
Any help is appreciated.
0
Comment
Question by:dodgerfan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24088822
Can you show how "newFolder" was created? Seems like you have a problem with the slashes of paths. If newFolder is a whole path, or if it starts out with a slash, you need a different approach.
0
 

Author Comment

by:dodgerfan
ID: 24089205
I've attached my complete function in a code snippet. I'm using Aspose slides for some of this.
Label2.text = "File Uploaded: " + hiddenValue;
string newPath = System.IO.Path.GetFileNameWithoutExtension(hiddenValue.Value);
string thumbnailPath = System.ConfigurationManager.AppSettings["uploads"];
 
Directory.CreateDirectory(thumbnailPath + newPath);
string FinalPath = thumbnailPath + newPath + "/";
 
Presentation srcPres = new Presentation(uploadPath + hiddenValue.Value);
int lastSlidePostion = srcPres.Slides.LastSlidePosition;
for (int i=1; i<= lastSlidePosition; i++)
{
Slide sld = srcPres.GetSlideByPosition(i);
double scaleX = 1.3;
double scaleY = 1.3;
System.Drawing.Image img = sld.GetThumbnail(scaleX, scaleY);
img.Save(String.Concat(finalPath, "Slide_", i.ToString().PadLeft(2, '0'), ".jpeg"), ImageFormat.Jpeg);
}
dlImages.DataSource = System.IO.Directory.GetFiles(Server.MapPath("~/Images/"));
dlImages.DataBind();

Open in new window

0
 

Expert Comment

by:aneelmehta
ID: 24095680
you are creating new folder in "thumbnailPath" and trying to access it in "images" folder are they both pointing to same location ? If yes then no need to use "Server.MapPath" as u have same path in string FinalPath = thumbnailPath + newPath + "/";
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:dodgerfan
ID: 24096622
I've tried using finalPath like this:
dlImages.DataSource = System.IO.Directory.GetFiles(Server.MapPath(finalPath);
It does not work. My datalist on the html page is below. The images/ is wrong now, correct? How do I fix that?

<asp:datalist id="dlImage" runat="server" RepeatDirection="Vertical">
  <ItemTemplate>
    <a href='<%= ResloveUrl("~/")images/<%# GetFileName(Container.DataItem.ToString()) %>' target="main">  
        <img src='<%= ResloveUrl("~/")images/<%# GetFileName(Container.DataItem.ToString()) %>' alt="Image" height="150" width="150" border="0" />
    </a>
  </ItemTemplate>
</asp:DataList>
0
 
LVL 39

Accepted Solution

by:
abel earned 500 total points
ID: 24097424
> Directory.CreateDirectory(thumbnailPath + newPath);

this is a dangerous line. Suppose thumbnailPath were "c:\temp" and newPath were "c:\my documents" then CreateDirectory will fail. Suppose one of them is relative, i.e., thumbnailPath is "c:\temp" and newPath is "somePath\OtherPath", then the CreateDirectory will also fail. I don't know what the variables contain, but you should try to make them correct (no leading slash or drive specs on newPath, always a trailing slash on thumbnailPath) before you combine them.

You seem to do the same with uploadPath, by doing new Presentation(uploadPath + hiddenValue.Value). The varaible uploadPath is not defined in the code above, so I cannot say much sensible about it, but again it can be slightly tricky to get right.

Later (where you use String.Concat instead of "+" to combine strings), you use finalPath. Which may not even exist if CreateDirectory had failed in the first place (but that would raise an error).

Finally, about your actual question, you look into a total different location than you have been storing the thumbnailes. (i.e., now you use Server.MapPath, which will look relative to your directory, which is by no means the same as the hiddenValue and thumbnailPath combined. If you intend to read back what you just saved, you should use something like:

dlImages.DataSource = System.IO.Directory.GetFiles(finalPath);

instead.
0
 

Author Comment

by:dodgerfan
ID: 24097608
abel, thanks for the insight and help. A lot of the code at this point is a work in progress as I try different things to get it working again. I have tried your suggestion before about the dlImages.DataSoure = System.IO.Directory.GetFiles(finalPath);
It does not work for some reson. Is there something else I should try?
0
 

Author Closing Comment

by:dodgerfan
ID: 31567589
Go it. Had to remove ResloveUrl from the html page calling the datalist. thanks.
0
 
LVL 39

Expert Comment

by:abel
ID: 24105262
> Go it. Had to remove ResloveUrl from the html page calling the datalist. thanks.

Good, glad you got it working in the end
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question