Solved

Server.MapPath and new folders

Posted on 2009-04-07
8
489 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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