2011-05-18 80 views
1
//aspx.cs file 


    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Web; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.IO; 

    public partial class trash : System.Web.UI.Page 
    { 
protected void Page_Load(object sender, EventArgs e) 
{ 
    Response.Write("<table style='width: 10px; height: 10px; margin-left:100px'>"); 

    foreach(var directory in new DirectoryInfo("C:\\Users\\naresh\\Documents\\Visual Studio 2010\\WebSites\\CMANAGER").GetDirectories()) 
    { 

Response.Write("<tr>");  
    Response.Write("<td><a href= view4.aspx?folder="+ directory.Name + "> "+ directory.Name +"</a></td>"); 

Response.Write("</tr>"); 
     } 
    Response.Write("</table>"); 
} 

}獲取動態路徑被點擊

有了這個代碼,我列出與hyperlinks.So指定目錄下的所有目錄,如果我的超級鏈接點擊現在我應該列出所有文件在一個單獨的頁面中的特定目錄。但我有問題在基於超鏈接點擊動態路徑。 請在這方面幫助我。 謝謝。

+0

你能指定問題嗎? – ibram 2011-05-18 20:33:58

回答

0

我想你想使用directory.FullName超鏈接。當你從查詢字符串讀取它時,你需要對它進行url編碼,然後在新頁面上進行解碼。

在view4.aspx讀取文件夾斷的查詢字符串後,再創建一個目錄信息對象和迭代directory.GetFiles的)結果(

這裏是一個DirectoryInfo類的鏈接以獲取更多信息 http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx

0

使用ASP.NET MVC 2(比把一切變成Page_Load好得多),你可以做這樣的:

HomeController.cs:

using System.IO; 
using System.Web.Mvc; 

namespace SO_web_directory.Controllers 
{ 
    public class HomeController : Controller 
    { 
     private static readonly string DefaultDirectory = @"C:\"; 

     public ActionResult Index(string path) 
     { 
      if (string.IsNullOrWhiteSpace(path)) 
       path = DefaultDirectory; 

      return View(new DirectoryInfo(path).GetDirectories()); 
     } 
    } 
} 

指數.aspx:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage<System.IO.DirectoryInfo[]>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Directories 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <table style='width: 10px; height: 10px; margin-left:100px'> 
    <% foreach (var directory in Model) 
     { %> 
     <tr> 
      <td> 
      <%= Html.ActionLink(
        directory.Name, "Index", 
        new RouteValueDictionary { { "path", directory.FullName } }) %> 
      </td> 
     </tr> 
     <% 
     }%> 
    </table> 
</asp:Content>