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();
        }
    }
}
相关推荐
Artech5 小时前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf2 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6252 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech2 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
LDR0063 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术3 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园3 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob3 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
源分享3 天前
Java线程同步的多种实现方法(非常详细)
java·开发语言·jvm
Luminous.3 天前
C语言--day30
c语言·开发语言