ASP.NET random images

S

Sahus Pilwal

Hi,

I hope someone can help me with this. I'm new to .NET and in fact server
side programming and have a small query I'm sure...

I'm using the System.IO Namespace with a For - each and If then statement to
randomly generate images to display in a <asp:image/> image control. The
random images are selected from a fixed folder on the webserver. All works
fine but only if the images in the folder are of image format say jpg or
gif. However sometimes a Thumb.db is created in (imgRotator folder)which of
course on random selection attempts to display. I want to be able to
restrict this so only jpg and gif formats can display in my image control,
just in case any other files are created in this folder either by machine or
human error.

The code I have so far is as follows. I'm using VB.NET

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Const IMGS_DIR As String = "imgRotator/"
Dim intFileLooper As String
Dim myDirInfo As DirectoryInfo
Dim myFileInfo As FileInfo
Dim arrFileInfo As Array
Dim RandomGenerator As New Random
Dim intFileNumberToUse As Integer

'Get directory info
myDirInfo = New DirectoryInfo(Server.MapPath(IMGS_DIR))

'Get file info
arrFileInfo = myDirInfo.GetFiles()

'Use count of files to generate a random nubmer from 1 to count
intFileNumberToUse = RandomGenerator.Next(1, arrFileInfo.Length + 1)

'Loop through the array of FileInfo objects until we ge to the right
one
intFileLooper = 1
For Each myFileInfo In arrFileInfo
If intFileLooper = intFileNumberToUse Then
Image1.ImageUrl = IMGS_DIR & myFileInfo.Name
Exit For
End If
intFileLooper = intFileLooper + 1
Next
End Sub

Any ideas/work arounds or "better way of doing" would be appreciated.

Regards,

Sas
 
F

fredericopatricio

Sahus said:
Hi,

I hope someone can help me with this. I'm new to .NET and in fact server
side programming and have a small query I'm sure...

I'm using the System.IO Namespace with a For - each and If then statement to
randomly generate images to display in a <asp:image/> image control. The
random images are selected from a fixed folder on the webserver. All works
fine but only if the images in the folder are of image format say jpg or
gif. However sometimes a Thumb.db is created in (imgRotator folder)which of
course on random selection attempts to display. I want to be able to
restrict this so only jpg and gif formats can display in my image control,
just in case any other files are created in this folder either by machine or
human error.

The code I have so far is as follows. I'm using VB.NET

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Const IMGS_DIR As String = "imgRotator/"
Dim intFileLooper As String
Dim myDirInfo As DirectoryInfo
Dim myFileInfo As FileInfo
Dim arrFileInfo As Array
Dim RandomGenerator As New Random
Dim intFileNumberToUse As Integer

'Get directory info
myDirInfo = New DirectoryInfo(Server.MapPath(IMGS_DIR))

'Get file info
arrFileInfo = myDirInfo.GetFiles()

'Use count of files to generate a random nubmer from 1 to count
intFileNumberToUse = RandomGenerator.Next(1, arrFileInfo.Length + 1)

'Loop through the array of FileInfo objects until we ge to the right
one
intFileLooper = 1
For Each myFileInfo In arrFileInfo
If intFileLooper = intFileNumberToUse Then
Image1.ImageUrl = IMGS_DIR & myFileInfo.Name
Exit For
End If
intFileLooper = intFileLooper + 1
Next
End Sub

Any ideas/work arounds or "better way of doing" would be appreciated.

Regards,

Sas

your code in C#:

string IMGS_DIR = "IMGS_DIR/";
DirectoryInfo myDirInfo = new
DirectoryInfo(Server.MapPath(IMGS_DIR));

Array arrFileInfo = myDirInfo.GetFiles();
Random RandomGenerator = new Random();
int intFileNumberToUse = RandomGenerator.Next(1,
arrFileInfo.Length + 1);



int intFileLooper = 1;
foreach(FileInfo myFileInfo in arrFileInfo)
{
if (intFileLooper == intFileNumberToUse)
{
Image1.ImageUrl =
myDirInfo.Name.ToString()+"/"+myFileInfo.Name.ToString();
}
intFileLooper++;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top