private void GetFolders() { //iterate through all folders in photo dir and bound to DataList //first we will get list of all folders,full path string gfolder=System.Configuration.ConfigurationSettings.AppSettings["photofolder"]; string[] folders=System.IO.Directory.GetDirectories(Server.MapPath(gfolder)); //get separate string as full parent name string parent=System.IO.Directory.GetParent(Server.MapPath(gfolder)).FullName+"\\"+gfolder+"\\"; ViewState["parent"]=parent; //we want to keep this value //string parent=System.IO.Directory.GetParent(Server.MapPath("photo")); //will remove parent path to leave only actual folders name to use them as a link for (int i=0; i<=folders.Length-1; i++) { folders[i]=folders[i].Remove(0,parent.Length);
} //bind source and get links to folders dlFolderList.DataSource=folders; dlFolderList.DataBind(); }
private void GetPhotos() { string photodir=System.Configuration.ConfigurationSettings.AppSettings["photofolder"]; //or get from config file string curGallery=""; // use convention G is parameters to switch between galleries if (Request["G"]!=null) { curGallery=Request["G"]; //find where we are } else { curGallery=System.IO.Directory.GetDirectories(Server.MapPath(photodir))[0]; //if no parameters show first directory string parent=(string) ViewState["parent"]; curGallery=curGallery.Substring(parent.Length); } // try get all files in folder try { //get thumbnails and photos arrays in current directory string[] photos=System.IO.Directory.GetFiles(Server.MapPath(photodir+"/"+curGallery),"*.jpg"); // this array used if we have separate files as thumbnail images. Convention here to add _thumb to file name //string[] thumb=System.IO.Directory.GetFiles(Server.MapPath("photo/"+curGallery),"*_thumb*"); //get directory info itself int i=System.IO.Directory.GetParent(Server.MapPath(photodir)).FullName.Length; // //get files names/ relative path //just get actual files names for thumbnails and photos //just removing extra path, leaving only catalog and file name for (int ix=0; ix<photos.Length; ix++) { photos[ix]=photos[ix].Substring(i+1); photos[ix]=photos[ix].Replace("\\","/"); } listImg.DataSource=photos; listImg.DataBind();
//find how many photos and how many pages to create pages int num=photos.Length;//it is number of photos //int num=thumb.Length; old usage lblPage.InnerText=num.ToString(); if (num>10) imgbNext.Visible=true;
//here we are using paged datasource PagedDataSource objPds = new PagedDataSource(); // objPds.DataSource = photos; //we will show thumbnails
// Indicate that the data should be paged objPds.AllowPaging = true;
// Set the number of items you wish to display per page objPds.PageSize = 10; //fixed page size, other option to control through drop down box. objPds.CurrentPageIndex = CurrentPage; ViewState["Pages"]=objPds.PageCount; //fill pages values for dropdown list //do it only first time if (!Page.IsPostBack) { dlPages.Items.Clear(); for (int ip=1;ip<=objPds.PageCount;ip++) { dlPages.Items.Add(ip.ToString()); } } else { //list already there, so just set the right page //dlPages.SelectedIndex=CurrentPage; } lblPage.InnerText=" Page "+(CurrentPage+1).ToString()+" of "+objPds.PageCount.ToString(); //control visibility of next/prev buttons imgbPrev.Enabled = !objPds.IsFirstPage; //imgbPrev.Visible=!objPds.IsFirstPage; imgbNext.Enabled = !objPds.IsLastPage; //imgbNext.Visible = !objPds.IsLastPage; repPhotoG.DataSource=objPds; // //DIV1.InnerText=thumb. //gallery.Add() repPhotoG.DataBind(); //we also will use item data bind events to add some links to thumbnails
} catch (Exception ex) { //do something with error }
当选择文件的时候,在选择文件下拉列表中显示文件夹下的所有txt文件.同时还要获取这时的txt的路径名,对它进行数据读取
private void GetFolders()
{
//iterate through all folders in photo dir and bound to DataList
//first we will get list of all folders,full path
string gfolder=System.Configuration.ConfigurationSettings.AppSettings["photofolder"];
string[] folders=System.IO.Directory.GetDirectories(Server.MapPath(gfolder));
//get separate string as full parent name
string parent=System.IO.Directory.GetParent(Server.MapPath(gfolder)).FullName+"\\"+gfolder+"\\";
ViewState["parent"]=parent; //we want to keep this value
//string parent=System.IO.Directory.GetParent(Server.MapPath("photo"));
//will remove parent path to leave only actual folders name to use them as a link
for (int i=0; i<=folders.Length-1; i++)
{
folders[i]=folders[i].Remove(0,parent.Length);
}
//bind source and get links to folders
dlFolderList.DataSource=folders; dlFolderList.DataBind();
}
通过它取到它下面所有文件夹
将它绑定到你的控件dlFolderList
他可以是datalist或者是dropdownlist
private void GetPhotos()
{ string photodir=System.Configuration.ConfigurationSettings.AppSettings["photofolder"]; //or get from config file string curGallery="";
// use convention G is parameters to switch between galleries
if (Request["G"]!=null)
{
curGallery=Request["G"]; //find where we are
}
else
{
curGallery=System.IO.Directory.GetDirectories(Server.MapPath(photodir))[0]; //if no parameters show first directory
string parent=(string) ViewState["parent"];
curGallery=curGallery.Substring(parent.Length);
}
// try get all files in folder
try
{
//get thumbnails and photos arrays in current directory
string[] photos=System.IO.Directory.GetFiles(Server.MapPath(photodir+"/"+curGallery),"*.jpg");
// this array used if we have separate files as thumbnail images. Convention here to add _thumb to file name //string[] thumb=System.IO.Directory.GetFiles(Server.MapPath("photo/"+curGallery),"*_thumb*");
//get directory info itself
int i=System.IO.Directory.GetParent(Server.MapPath(photodir)).FullName.Length;
//
//get files names/ relative path
//just get actual files names for thumbnails and photos
//just removing extra path, leaving only catalog and file name
for (int ix=0; ix<photos.Length; ix++)
{
photos[ix]=photos[ix].Substring(i+1);
photos[ix]=photos[ix].Replace("\\","/");
}
listImg.DataSource=photos;
listImg.DataBind();
//find how many photos and how many pages to create pages
int num=photos.Length;//it is number of photos
//int num=thumb.Length; old usage
lblPage.InnerText=num.ToString();
if (num>10) imgbNext.Visible=true;
//here we are using paged datasource
PagedDataSource objPds = new PagedDataSource();
//
objPds.DataSource = photos; //we will show thumbnails
// Indicate that the data should be paged
objPds.AllowPaging = true;
// Set the number of items you wish to display per page
objPds.PageSize = 10; //fixed page size, other option to control through drop down box.
objPds.CurrentPageIndex = CurrentPage;
ViewState["Pages"]=objPds.PageCount;
//fill pages values for dropdown list
//do it only first time
if (!Page.IsPostBack)
{
dlPages.Items.Clear();
for (int ip=1;ip<=objPds.PageCount;ip++)
{
dlPages.Items.Add(ip.ToString());
}
}
else
{
//list already there, so just set the right page
//dlPages.SelectedIndex=CurrentPage;
} lblPage.InnerText=" Page "+(CurrentPage+1).ToString()+" of "+objPds.PageCount.ToString();
//control visibility of next/prev buttons
imgbPrev.Enabled = !objPds.IsFirstPage;
//imgbPrev.Visible=!objPds.IsFirstPage;
imgbNext.Enabled = !objPds.IsLastPage;
//imgbNext.Visible = !objPds.IsLastPage;
repPhotoG.DataSource=objPds;
//
//DIV1.InnerText=thumb.
//gallery.Add()
repPhotoG.DataBind();
//we also will use item data bind events to add some links to thumbnails
}
catch (Exception ex)
{
//do something with error }
//imgLarge.Attributes["onfilterchange"]="fade()";
}我这个代码使用来取到文件夹下面的图片文件,同时绑定显示在页面上,图片所在的根文件夹是在webconfig当中配置好了的,楼主同学可以参考,不建议你在页面当中把真实路径显示出来,从安全的角度来说实在是不恰当,如果楼主果真需要那么做最笨的办法就是拼接字符串,再者就是单独写一个函数gettxturl()实质上也是起到拼接串的作用
我现在想获得文件的路径.然后在其他button中读取数据...谢谢了
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string path = "" + DropDownList1.SelectedItem.Text + ":\\" + DropDownList2.SelectedItem.Text + "\\" + DropDownList3.SelectedIndex;
StreamReader reader = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
string oneline;
DataTable dt = new DataTable();
dt.Columns.Add("dbID", typeof(string));
dt.Columns.Add("商业站点", typeof(string));
while ((oneline = reader.ReadLine()) != null)
{
DataRow row = dt.NewRow();
oneline = oneline.Trim();
if (oneline != "")
{
string[] ss = oneline.Split(".".ToCharArray());
row[0] = ss[0];
row[1] = ss[1];
dt.Rows.Add(row.ItemArray);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch
{
Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('文件格式不符合');</script>");
}
}要调用这个文件的path