本文摘自PHP中文网,作者黄舟,侵删。
这篇文章主要为大家详细介绍了C# 遍历文件夹及子目录下所有图片的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下要求:取指定目录下面的所有图片,以表格的型式展示并显示该图片的相对路径。
服务端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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" );
var images = Directory.GetFiles(path, "." , SearchOption.AllDirectories).Where(s => s.EndsWith( ".png" ) || s.EndsWith( ".jpg" ) || s.EndsWith( ".gif" ));
foreach ( var i in images){
var str = i.Replace(path1, "" );
var path2 = str.Replace( "\\" , "/" );将字符“\\”转换为“/”
ja.Add(path2);
}
info = Newtonsoft.Json.JsonConvert.SerializeObject(ja);
}
}
|
前端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <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#遍历文件夹及子目录下所有图片的示例代码分享的详细内容!
相关阅读 >>
C#基础入门-关键字的介绍
C#tuples(元组)
详细介绍data url生成工具C#版第二版的示例代码
C#如何使用浏览按钮获得文件路径和文件夹路径的实现方法
C#生成一万以内所有不重复数字的四位数
C#将unicode编码转换为汉字字符串的代码分析
C# winform程序上传图片到指定目录的示例代码
C# web应用调试开启外部访问的步骤
C#如何连接数据库?oledbconnection与sqlconnection的区别
分析C#httpwebrequest访问https错误处理的方法
更多相关阅读请进入《C#》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » 实现C#遍历文件夹及子目录下所有图片的示例代码分享