C# 遍歷文件夾子目錄下所有圖片及遍歷文件夾下的文件
要求:取指定目錄下面的所有圖片,以表格的型式展示并顯示該圖片的相對(duì)路徑。
服務(wù)端代碼:
public partial class ViewIcon : System.Web.UI.Page
{
JArray ja = new JArray(); //定義一個(gè)數(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個(gè)字符串組合成路徑
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, "");//獲取相對(duì)路徑
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盤下,代碼如下。將文件名輸出到一個(gè)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)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C#實(shí)現(xiàn)向數(shù)組指定索引位置插入新的元素值
這篇文章給大家介紹了利用C#實(shí)現(xiàn)向數(shù)組指定索引位置插入新的元素值,首先需要定義一個(gè)一維數(shù)組,然后修改數(shù)組的長(zhǎng)度,從而在其中增加一個(gè)元素,需要的朋友可以參考下2024-02-02
關(guān)于C#?調(diào)用Dll?傳遞字符串指針參數(shù)的問題
這篇文章主要介紹了C#?調(diào)用Dll傳遞字符串指針參數(shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
C#操作SQLite數(shù)據(jù)庫之讀寫數(shù)據(jù)庫的方法
這篇文章主要介紹了C#操作SQLite數(shù)據(jù)庫之讀寫數(shù)據(jù)庫的方法,簡(jiǎn)單分析了C#針對(duì)SQLite數(shù)據(jù)庫的讀寫及顯示等操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
c#設(shè)計(jì)模式之單例模式的實(shí)現(xiàn)方式
這篇文章主要給大家介紹了關(guān)于c#設(shè)計(jì)模式之單例模式的實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
C#正則匹配RegexOptions選項(xiàng)的組合使用方法
本文主要簡(jiǎn)單介紹RegexOptions各種選項(xiàng)的作用,并介紹如何組合使用,為初學(xué)者解除一些疑惑。2016-04-04
C#面向?qū)ο笤O(shè)計(jì)原則之組合/聚合復(fù)用原則
這篇文章介紹了C#面向?qū)ο笤O(shè)計(jì)原則之組合/聚合復(fù)用原則,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
C#對(duì)XML文件的各種操作實(shí)現(xiàn)方法
C#對(duì)XML文件的各種操作實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-04-04

