I am creating a website using asp.net and c#
I need to access all files one by one in a directory. So I wrote the following code.
string[] ssImgs = Directory.GetFiles("images/movies");
Label1.Text = ssImgs[0];
Label2.Text = ssImgs[1];
But it gives an error. What is the correct way to do this? Thanks in advance.
A little advice, when using the file system in ASP.NET, leverage
Server.MapPath
:where
~/images/movies
is the full relative path from the root of the web application.Secondly, before just grabbing an index of the array, make sure it's actually there. It would be better to change your indexing code to something like this:
By making these two changes you won't have to worry about getting a
DirectoryNotFound
exception or anIndexOutOfRange
exception.