2017-10-11 35 views
1

我需要將文件夾中的文件名顯示到GridView控件中。我想用目錄類在ASP.Net的Gridview控件中獲取文件名的文件夾c#

在我的數據庫我有列sFolder按每行在此值:

control/Imp/foo 

我已經嘗試了本tutorial在網絡上,但我不能從一個文件夾獲取文件名成GridView控件。

我沒有錯誤,但即使文件夾路徑正確,GridView也是空的。

我的代碼如下。

你能幫我嗎?

預先感謝您的任何幫助,非常感謝

的.cs

dt2 = new DataTable(); 
ds2 = new DataSet(); 

sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); "); 

using (OdbcConnection cn = 
    new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString)) 
{ 
    using (OdbcCommand cmd = 
     new OdbcCommand(sql, cn)) 
    { 

     OdbcDataAdapter adapter = 
      new OdbcDataAdapter(cmd); 
     adapter.Fill(ds2); 

     if (ds2.Tables.Count > 0) 
     { 
      dt2 = ds2.Tables[0]; 
      FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\')); 
      Response.Write(FilePath); 

// the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo // 

      string[] filesLoc = Directory.GetFiles(FilePath); 
      List<ListItem> files = new List<ListItem>(); 
      foreach (string file in filesLoc) 
      { 
       files.Add(new ListItem(Path.GetFileName(file))); 
      } 
      gvDownload.DataSource = files; 
      gvDownload.DataBind(); 

     } 
    } 
} 

return ds2; 

的.aspx

<asp:GridView ID="gvDownload" EmptyDataText="Data empty" 
    runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" GridLines="Vertical"> 
    <AlternatingRowStyle BackColor="#DCDCDC" /> 
    <Columns> 
     <asp:BoundField DataField="Text" HeaderText="FileName" /> 
    </Columns> 
</asp:GridView> 

#Edit 01

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      BindData(); 
     } 
    } 


    private void BindData() 
    { 
     RetrieveProductsDowload(); 
    } 

    private DataSet RetrieveProductsDowload() 
    { 
dt2 = new DataTable(); 
ds2 = new DataSet(); 

sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); "); 

using (OdbcConnection cn = 
    new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString)) 
{ 
    using (OdbcCommand cmd = 
     new OdbcCommand(sql, cn)) 
    { 

     OdbcDataAdapter adapter = 
      new OdbcDataAdapter(cmd); 
     adapter.Fill(ds2); 

     if (ds2.Tables.Count > 0) 
     { 
      dt2 = ds2.Tables[0]; 
      FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\')); 
      Response.Write(FilePath); 

// the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo // 

      string[] filesLoc = Directory.GetFiles(FilePath); 
      List<ListItem> files = new List<ListItem>(); 
      foreach (string file in filesLoc) 
      { 
       files.Add(new ListItem(Path.GetFileName(file))); 
      } 
      gvDownload.DataSource = files; 
      gvDownload.DataBind(); 

     } 
    } 
} 

return ds2; 

} 
+0

嘗試:'gvDownload.DataSource = Directory.GetFiles(文件路徑); gvDownload.DataBind();',使用'AutoGenerateColumns =「True」',並從GridView中刪除' ...'。它工作嗎? – Ritesh

+0

@Ritesh感謝你的回覆,但不工作,GV是空的。 –

+0

您是否在.cs文件中使用'Page_Load'來編寫此代碼? – Ritesh

回答

2

請試試這個:

string[] allfiles = Directory.GetFiles(FilePath, "*", SearchOption.AllDirectories); 
gvDownload.DataSource = allfiles; 
gvDownload.DataBind(); 
相關問題