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

来源:互联网 发布:光大证券金阳光软件 编辑:程序博客网 时间:2024/05/28 11:49

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>");
            }
    }

 

效果:

原创粉丝点击