C# 遍歷文件夾子目錄下所有圖片及遍歷文件夾下的文件
要求:取指定目錄下面的所有圖片,以表格的型式展示并顯示該圖片的相對路徑。
服務(wù)端代碼:
 public partial class ViewIcon : System.Web.UI.Page
 {
  JArray ja = new JArray(); //定義一個數(shù)組
  public string info = string.Empty; 
  protected void Page_Load(object sender, EventArgs e)
  {
   var path1 = System.AppDomain.CurrentDomain.BaseDirectory;//獲取程序集目錄
   string path = Path.Combine(path1, "Image", "menu");//Path.Combine 將3個字符串組合成路徑
   var images = Directory.GetFiles(path, ".", SearchOption.AllDirectories).Where(s => s.EndsWith(".png") || s.EndsWith(".jpg") || s.EndsWith(".gif"));
   //images = Directory.GetFiles(path, "*.png|*.jpg", SearchOption.AllDirectories);
   //Directory.GetFiles 返回指定目錄的文件路徑 SearchOption.AllDirectories 指定搜索當(dāng)前目錄及子目錄
   //遍歷string 型 images數(shù)組
   foreach (var i in images){
    var str = i.Replace(path1, "");//獲取相對路徑
    var path2 = str.Replace("\\", "/");將字符“\\”轉(zhuǎn)換為“/”
    ja.Add(path2);
   }
   info = Newtonsoft.Json.JsonConvert.SerializeObject(ja);//序列化為String
  }
 }
前端代碼:
<script type="text/javascript">
  $(function(){
   var images = <%=info%>;
  var list = [];
  list.push("<table>");
  list.push("<thead>"); 
  list.push("<tr>"); 
  list.push("<td>圖標(biāo)</td>"); 
  list.push("<td>路徑</td>"); 
  list.push("<td>圖標(biāo)</td>"); 
  list.push("<td>路徑</td>");
  list.push("</tr>"); 
  list.push("</thead>");
  list.push("<tbody>");
  $.each(images, function (a,b) {
   if((a+1)%2==0){
    list.push("<td>"+"<img width='50' height='50' src = '../../" + b + "'></td>");
    list.push("<td>"+b+"</td>");
    list.push("</tr>"); 
   }
   if((a+1)%2!=0){
    list.push("<tr>"); 
    list.push("<td>"+"<img width='50' height='50' src = '../../" + b + "'></td>");
    list.push("<td>"+b+"</td>");
   } 
  })
  list.push("</tbody>");
  list.push("</table>");
  list.push("<br>");
  var images = list.join("");
  $("#imgs").append(images); 
 })
</script>
效果圖如下:
下面給大家介紹下C# 遍歷文件夾下所有子文件夾中的文件,得到文件名
假設(shè)a文件夾在F盤下,代碼如下。將文件名輸出到一個ListBox中
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void button2_Click(object sender, EventArgs e)
    {
      DirectoryInfo theFolder = new DirectoryInfo(@"F:\a\");
      DirectoryInfo[] dirInfo = theFolder.GetDirectories();
      //遍歷文件夾
      foreach (DirectoryInfo NextFolder in dirInfo)
      { 
        // this.listBox1.Items.Add(NextFolder.Name);
        FileInfo[] fileInfo = NextFolder.GetFiles();    
        foreach (FileInfo NextFile in fileInfo) //遍歷文件
        this.listBox2.Items.Add(NextFile.Name); 
      }
    }
  }
}
以上所述是小編給大家介紹的C# 遍歷文件夾及子目錄下所有圖片的實(shí)現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
上一篇:C#的Socket實(shí)現(xiàn)UDP協(xié)議通信示例代碼
欄 目:C#教程
下一篇:C#把UNICODE編碼轉(zhuǎn)換為GB編碼的實(shí)例
本文標(biāo)題:C# 遍歷文件夾子目錄下所有圖片及遍歷文件夾下的文件
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5955.html
您可能感興趣的文章
- 01-10C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
 - 01-10C#遍歷文件夾后上傳文件夾中所有文件錯誤案例分析
 - 01-10C#編程實(shí)現(xiàn)獲取文件夾中所有文件的文件名
 - 01-10C#中用foreach語句遍歷數(shù)組及將數(shù)組作為參數(shù)的用法
 - 01-10C#程序中創(chuàng)建、復(fù)制、移動、刪除文件或文件夾的示例
 - 01-10Windows中使用C#為文件夾和文件編寫密碼鎖的示例分享
 - 01-10C#如何遍歷Dictionary
 - 01-10C#中哈希表(HashTable)用法實(shí)例詳解(添加/移除/判斷/遍歷/排序等
 - 01-10C#簡單遍歷指定文件夾中所有文件的方法
 - 01-10C#遍歷集合與移除元素的方法
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
 - 01-10C#中split用法實(shí)例總結(jié)
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 


