<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.IO" %>
<%
//path to folder with snippet
string folderPath = "/highschool/sortsnippets_hshome/";
//load file information from target folder
FileInfo fi = new FileInfo(Server.MapPath(folderPath));
DirectoryInfo di = fi.Directory;
FileSystemInfo[] fsi = di.GetFiles();
//get number of files in directory
int intFileCount = fsi.Length;
//choose item to display at random
int intRndEnd = 1;
if (intFileCount > 1){
intRndEnd = intFileCount + 1;
}
Random rndSelected = new Random();
int intRndSelected = rndSelected.Next(1, intRndEnd);
//load filename of selected item
string strSelectedFileName = "";
int fsCount = 1;
//loop through the FileSystemInfo class to pull the selected file name. There are other ways to accomplish this, but this way is the simplest.
foreach (FileSystemInfo info in fsi)
{
if (intRndSelected == fsCount)
{
strSelectedFileName = info.Name;
}
fsCount++;
}
//load streamreader to pull contents of file
string strFileFullPath = Server.MapPath(folderPath) + strSelectedFileName;
StreamReader re = File.OpenText(strFileFullPath);
//pull file contents into var
string strFileContents = re.ReadToEnd();
//close reader
re.Close();
//write string to page
Response.Write(strFileContents);
%>