真爱无限的知识驿站

学习积累技术经验,提升自身能力

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

 

 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-BlogPHP 1.7.3

Copyright 2024-2027 pukuimin Rights Reserved.
粤ICP备17100155号