C#中读取文件夹(包含固定字样文件名)

C#中读取文件

复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
            //设定读取文件名包含的字样,可调整为动态传值
                string abc = "字样";
			//获取当前时间
                DateTime now = DateTime.Now;
			//获取当前年月
                string yearM = now.Year + "." + now.Month.ToString("D2");
			//设定读取路径下的文件后缀
                string[] searchPatterns = { ".jpg", ".bmp", ".png" };
			//设定读取路径
                string[] searchFolders = { @"\\127.0.0.1\读取路径 };

			//循环路径
                foreach (var folder in searchFolders)
                {
			//循环路径下不同后缀文件
                    foreach (var pattern in searchPatterns)
                    {
                        string[] files = Directory.GetFiles(folder, "*" + abc + "*" + pattern);

                        if (files.Length > 0)
                        {
                            foreach (string file in files)
                            {
                            //打印文件夹名
                                Console.WriteLine(Path.GetFileName(file));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("發生了錯誤: " + ex.Message);
            }


            Console.Read();
        }
    }
}
相关推荐
临床数据科学和人工智能兴趣组2 小时前
在R语言中, 使用 as.factor() 函数转换数值型变量
开发语言·r语言
andyweike5 小时前
php笔记
开发语言·笔记·php
Circ.6 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
波力海苔夹心脆6757 小时前
C# 序列化与反序列化详解:System.Text.Json、Newtonsoft.Json、XmlSerializer 用法、特性选项与安全实践
经验分享·后端·c#·json·.net
现任明教教主~8 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
Zhou1411368 小时前
SpringSecurity_02_授权与高级功能
开发语言·python
码匠许师傅9 小时前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http
小小小小钰儿10 小时前
1.5-Python网络编程
开发语言·网络·python·web安全·计算机·网络安全