用 System.IO.Directory 类/// <summary>
/// 获取文件集合
/// </summary>
/// <param name="fullPath">当前目录路径</param>
/// <returns></returns>
public FileInfo[] GetFiles( string fullPath )
{
DirectoryInfo curDir = new DirectoryInfo( fullPath );
FileInfo[] fileInfo = curDir.GetFiles();
return fileInfo;
}取后缀名为.gif, .jpg ,等结尾的文件。这样很简单就可以知道你这些图片的路径。
给 image 控件赋值路径。
/// 获取文件集合
/// </summary>
/// <param name="fullPath">当前目录路径</param>
/// <returns></returns>
public FileInfo[] GetFiles( string fullPath )
{
DirectoryInfo curDir = new DirectoryInfo( fullPath );
FileInfo[] fileInfo = curDir.GetFiles();
return fileInfo;
}取后缀名为.gif, .jpg ,等结尾的文件。这样很简单就可以知道你这些图片的路径。
给 image 控件赋值路径。
public static void AttachImage( Page form, string szPath )
{ string[] szFiles = Directory.GetFiles( szPath, "*.jpg" ); int pos = -1;
string szOriginalImagePath = string.Empty; //form
form.Response.Write( "<br><br><br><br><hr>" ); foreach( string szFilePath in szFiles )
{
System.Web.UI.WebControls.Image webImg = new System.Web.UI.WebControls.Image();
System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label(); pos = szFilePath.LastIndexOf('\\');
szOriginalImagePath = szFilePath.Substring( pos + 1 );
webImg.ImageUrl = "name\picture\pic\che\" + szOriginalImagePath;
webImg.Width = 88;
webImg.Height = 58;
lbl.Text = " "; form.Controls.Add( webImg );
form.Controls.Add( lbl );
}
}//调用,比如
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
AttachImage( this, Server.MapPath(".\name\picture\pic\che\") );
}