1 point Member
propoo
Joined on 08-31-2007, 10:32 PM
Posts 3
Hey all ,
I am using following code to check if there is any image into my folder and if there are images it should load name into the listview. To do that I am first checking if there are any image and I am saving that name into datatable and finally assigining datatable as a source to listview.
My problem is it shows all the names of imags as first or single record in listview whereas if i try to display in label is shows a break
Result for label
Blue hills.jpg
Sunset.jpg
Water lilies.jpg
Winter.jpg
Result for listview
Blue hills.jpg<br/>Sunset.jpg<br/>Water lilies.jpg<br/>Winter.jpg<br/>
Expand|Select|Wrap|Line Numbers
- using System.IO;
- public partial class Image : System.Web.UI.Page
- {
- string name="";
- string strDriveLetter = "C:/";
- string strDefaultPath = "Documents and Settings/All Users/Documents/My Pictures/Sample Pictures";
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- DataTable myDt = new DataTable();
- myDt = CreateDataTable();
- Session["myDatatable"] = myDt;
- this.ListBox1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
- this.ListBox1.DataBind();
- }
- string strPath;
- // check if the query string variable "strPath" has a value
- // if it does, we are navigating a folder that is under the root folder
- // if it does not, we start at the root folder by default
- if (Request.QueryString["strPath"] != null)
- {
- strPath = Request.QueryString["strPath"];
- }
- else
- {
- strPath = strDefaultPath;
- }
- DisplayDirContents(strPath);
- AddDataToTable(this.name.ToString(), (DataTable)Session["myDatatable"]);
- this.ListBox1.DataSource = ((DataTable)Session["myDatatable"]).DefaultView;
- this.ListBox1.DataBind();
- }
- private void DisplayDirContents(string strPath)
- { // function to display list of folders and files in specified path
- // define some arrays
- DirectoryInfo[] aryDirectories;
- FileInfo[] aryFiles;
- // variable to store the complete file system path to the folder
- string strFSPath = strDriveLetter + strPath;
- // some string manipulation to get the name of the current script
- string strCurrentScript = Request.ServerVariables["URL"];
- string[] arrURL = strCurrentScript.Split('/');
- strCurrentScript = arrURL[arrURL.Length - 1];
- // instantiate local instance of DirectoryInfo object
- DirectoryInfo objCurrentDirectory = new DirectoryInfo(strFSPath);
- // get the directories present in the current folder
- // as an array of DirectoryInfo objects
- aryDirectories = objCurrentDirectory.GetDirectories();
- // get the files present in the current folder
- // as an array of FileInfo objects
- aryFiles = objCurrentDirectory.GetFiles("*.jpg");
- // check if the current folder has any files
- if (aryFiles.Length != 0)
- {
- // if it does, then iterate and
- // display the information about each file
- foreach (FileInfo objFile in aryFiles)
- {
- name += objFile.ToString();
- name += "<br/>";
- Label1.Text += objFile.ToString();
- Label1.Text += "<br/>";
- }
- }
- }
- private DataTable CreateDataTable()
- {
- DataTable myDataTable = new DataTable();
- DataColumn myDataColumn;
- myDataColumn = new DataColumn();
- myDataColumn.DataType = Type.GetType("System.String");
- myDataColumn.ColumnName = "name";
- myDataTable.Columns.Add(myDataColumn);
- return myDataTable;
- }
- private void AddDataToTable(string name, DataTable myTable)
- {
- DataRow row;
- row = myTable.NewRow();
- row["name"] = name.ToString();
- myTable.Rows.Add(row);
- }
- }