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();
        }
    }
}
相关推荐
先知后行。33 分钟前
QT实现计算器
开发语言·qt
掘根33 分钟前
【Qt】常用控件3——显示类控件
开发语言·数据库·qt
西阳未落4 小时前
C++基础(21)——内存管理
开发语言·c++·面试
我的xiaodoujiao5 小时前
Windows系统Web UI自动化测试学习系列2--环境搭建--Python-PyCharm-Selenium
开发语言·python·测试工具
callJJ5 小时前
从 0 开始理解 Spring 的核心思想 —— IoC 和 DI(2)
java·开发语言·后端·spring·ioc·di
hsjkdhs6 小时前
万字详解C++之构造函数析构函数
开发语言·c++
Lin_Aries_04217 小时前
容器化简单的 Java 应用程序
java·linux·运维·开发语言·docker·容器·rpc
techdashen7 小时前
12分钟讲解Python核心理念
开发语言·python
山海不说话8 小时前
Java后端面经(八股——Redis)
java·开发语言·redis
郝学胜-神的一滴8 小时前
谨慎地迭代函数所收到的参数 (Effective Python 第31条)
开发语言·python·程序人生·软件工程