asp.net 中显示文件夹下的文件,并有链接地址的例子

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Web;

 

    protected void Page_Load(object sender, EventArgs e)
    {
        display_files("/j1", "*.asp");  //"all" 代表全部类型的文件
    }
    public void display_files(string path,string type)
    {
        string fpath = Server.MapPath(path);
        pttt.Text = fpath;
        if (type == "all")
        {
            type = "*.*";
        }
            string[] files = Directory.GetFiles(fpath,type, SearchOption.TopDirectoryOnly);
            foreach (string file in files)
            {
                string fname = Path.GetFileNameWithoutExtension(file);
                //list1.Items.Add(file);
               // string http = Request.ServerVariables("server_name");
                Response.Write ("<a target='_blank' href=http://localhost/"+path+'/'+Path.GetFileName(file)+">"+Path.GetFileName(file)+"</a><br>");
            }
    }

 

效果:

posted @ 2012-06-23 14:28  真爱无限  阅读(228)  评论(0编辑  收藏  举报