I'm stuck trying to figure out how to print out the images I'm storing in a directory in ASP/C#
I found this, but the problem is it prints out C:\\Visual Studios 2010\Projects\MyTestUploader\Files\img001.jpg
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
Label1.Text += "<img src=\"" + fileName + "" /><br />";
}
For now I just want to simply print out all the images in the directory. I'll worry about formatting them nicely, later :)
The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).
We'll use "cd" to move down as well as up the directory structure. The second way to list files in a directory, is to first move into the directory using the "cd" command (which stands for "change directory", then simply use the "ls" command.
Navigate directories. Open a window, double-click on a folder, and then double-click on a sub-folder. Use the Back button to backtrack. The cd (change directory) command moves you into a different directory.
You can use Path.GetFileName
to get the file name and extension of the specified path, whilst excluding the directory path (which is presumably what you want).
string[] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
Label1.Text += "<img src=\"" + Path.GetFileName(fileName) + "\" /><br />";
}
For example, calling
Path.GetFileName(@"C:\Visual Studios 2010\Projects\MyTestUploader\Files\img001.jpg")
would return "img001.jpg"
.
Note that you were also missing a \
to escape the "
at the end of your attribute value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With