在上个项目开发中遇到这样的需求,取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径。下面小编给大家分享C# 遍历文件夹子目录下所有图片及遍历文件夹下的文件,一起看看吧
要求:取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径。
服务端代码:
public partial class ViewIcon : System.Web.UI.Page { JArray ja = new JArray(); //定义一个数组 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数组 foreach (var i in images){ var str = i.Replace(path1, "");//获取相对路径 var path2 = str.Replace("\\", "/");将字符“\\”转换为“/” 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 src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/194/526d665f9f3910f2b6896b625f164f54-0.jpg?x-oss-process=image/resize,p_40" class="lazy" style="max-width:90%" + b + "' alt="C#遍歷資料夾子目錄下所有圖片及遍歷資料夾下的檔案程式碼分享" ></td>"); list.push("<td>"+b+"</td>"); list.push("</tr>"); } if((a+1)%2!=0){ list.push("<tr>"); list.push("<td>"+"<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/194/526d665f9f3910f2b6896b625f164f54-0.jpg?x-oss-process=image/resize,p_40" class="lazy" style="max-width:90%" + b + "' alt="C#遍歷資料夾子目錄下所有圖片及遍歷資料夾下的檔案程式碼分享" ></td>"); list.push("<td>"+b+"</td>"); } }) list.push("</tbody>"); list.push("</table>"); list.push("<br>"); var images = list.join(""); $("#imgs").append(images); }) </script>
效果图如下:
下面给大家介绍下C# 遍历文件夹下所有子文件夹中的文件,得到文件名
假设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#遍歷資料夾子目錄下所有圖片及遍歷資料夾下的檔案程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C#和.NET的關係是密不可分的,但它們不是一回事。 C#是一門編程語言,而.NET是一個開發平台。 C#用於編寫代碼,編譯成.NET的中間語言(IL),由.NET運行時(CLR)執行。

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3 Linux新版
SublimeText3 Linux最新版

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器