Slapping Together a Photo Gallery in ASP.NET Part II - Working with Files
(Page 2 of 7 )
The first example in this article introduces the File() object. This is a counterpart of the Directory() object with which you're already familiar.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<SCRIPT language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
// path to an image file
string strPhotoFilePath = "E:\\inetpub\\wwwroot\\Gallery\\
Australia\\DSCN3944.JPG";
// check if file exists
if(File.Exists(strPhotoFilePath)) {
output.Text = "The file <U>" + strPhotoFilePath + "</U> exists.";
} else {
output.Text = "Sorry, the file <U>" + strPhotoFilePath + "</U> does not exist.";
}
}
</SCRIPT>
<HTML>
<HEAD>
<TITLE>My Gallery</TITLE>
<BASEFONT face="Arial" />
</HEAD>
<BODY>
<asp:Label id="output" runat="server" />
</BODY>
</HTML>
Once again, I have to deal with two familiar scenarios:
the specified file either exists on the server...

... or it does not.

I've already stated that the File() object is a counterpart of the Directory() object. So, it goes without saying that it is necessary to "import" the "System.IO" assembly into my ASP.NET script.
The "strPhotoFilePath" string variable is used to store the path to the file that I wish to access. The all-new File() object comes with list of static methods; I've invoked the Exists() method, whose behavior is similar to the Exists() method of the Directory() object. This method returns a "true" value if the file (or directory) exists, and a "false" value otherwise.
While the above example may not necessarily trouble your gray cells, the example in the next section will definitely demonstrate the usefulness of this innocuous File() object.
So, what are you waiting for? Flip the page and get moving!
Next: Of Albums and Photos >>
More ASP.NET Articles
More By Harish Kamath