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 指定搜索當前目錄及子目錄
//遍歷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>圖標</td>");
list.push("<td>路徑</td>");
list.push("<td>圖標</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# 遍歷文件夾及子目錄下所有圖片的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
C#數(shù)據(jù)結(jié)構(gòu)之單鏈表(LinkList)實例詳解
這篇文章主要介紹了C#數(shù)據(jù)結(jié)構(gòu)之單鏈表(LinkList)實現(xiàn)方法,結(jié)合實例形式較為詳細的分析了單鏈表的原理、定義與C#具體實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
C#使用SqlServer作為日志數(shù)據(jù)庫的設(shè)計與實現(xiàn)
這篇文章主要給大家介紹了關(guān)于C#使用SqlServer作為日志數(shù)據(jù)庫的設(shè)計與實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
C# 從Excel讀取數(shù)據(jù)向SQL server寫入
這篇文章主要介紹了C# 從Excel讀取數(shù)據(jù)向SQL server寫入的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
詳解C#中的System.Timers.Timer定時器的使用和定時自動清理內(nèi)存應(yīng)用
這篇文章主要介紹了詳解C#中的System.Timers.Timer定時器的使用和定時自動清理內(nèi)存應(yīng)用,需要的朋友可以參考下2017-06-06
C#實現(xiàn)按照指定長度在數(shù)字前補0方法小結(jié)
這篇文章主要介紹了C#實現(xiàn)按照指定長度在數(shù)字前補0方法,實例總結(jié)了兩個常用的數(shù)字補0的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04

