Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Server.MapPath and new folders

Posted on 2009-04-07
8
Medium Priority
?
499 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

730 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