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();
        }
    }
}
相关推荐
大可不必了6 小时前
03 函数定义和调用
开发语言·python
I_belong_to_jesus6 小时前
C++关于分支预测的优化1
开发语言·c++·算法
承渊政道7 小时前
【Python编程—从入门到实践】(记分与闯关:用Python+Pygame,打造越战越精彩的太空大战!)
开发语言·python·pycharm·游戏程序·pygame
爱学习的程序媛7 小时前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
eggwyw9 小时前
Anaconda安装卸载的详细流程指南
开发语言·python
用户7884773163412 小时前
WinForms 迁移 WPF:Upgrade Assistant 覆盖 30%,剩下 70% 我手写了六周
c#
another heaven13 小时前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++
SomeOtherTime15 小时前
Rust 闭包语法和原理(AI回答)
开发语言·后端·rust
apcipot_rain15 小时前
C++转Java日记(10)
java·开发语言
总要学点什么16 小时前
第105篇:三维特效-流动图片墙体效果——让箭头沿墙长滚,它俩竟是同一份代码
开发语言·javascript·ecmascript·gis·webgl·材质·cesium