Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Server.MapPath and new folders

Posted on 2009-04-07
8
Medium Priority
?
501 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

885 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