广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C# 获取文件夹里所有文件名的详细代码
  • 396
分享到

C# 获取文件夹里所有文件名的详细代码

C# 获取文件夹文件名C# 获取文件夹里所有文件名 2022-11-13 14:11:11 396人浏览 八月长安
摘要

主要是这个方法 List<string> GetAllFileNames(string path,string pattern="*") {

主要是这个方法

 List<string> GetAllFileNames(string path,string pattern="*")
        {
            List<FileInfo> folder  = new DirectoryInfo(path).GetFiles(pattern).ToList();

            return folder.Select(x=>x.Name).ToList();
        }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.windows.FORMs;
namespace GetFileNames
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            textBox1.Text = @"D:\jdScript\jdpro-main";
            textBox2.Text = "*.js";
        }

        string _outFileName = "aaaa.bat";

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var fileName in GetAllFileNames(textBox1.Text))
            {
                richTextBox1.AppendText("node " + fileName + '\n');
            }

            OutputFile(textBox1.Text + "\"+ _outFileName, richTextBox1.Text);
        }

        void OutputFile(string strFilePath,string strContent)
        {
            StreamWriter swOut = new StreamWriter(strFilePath, false, Encoding.Default);
            swOut.WriteLine(strContent);
            swOut.Flush();
            swOut.Close();
        }

        List<string> GetAllFileNames(string path,string pattern="*")
        {
            List<FileInfo> folder  = new DirectoryInfo(path).GetFiles(pattern).ToList();

            return folder.Select(x=>x.Name).ToList();
        }

    }
}

补充:C# 正则表达式分组查询

result[1] = "memory usage threshold: 80 %";
strResult = Regex.Match(result[1], @"(?<a>\d+)\s*%").Groups["a"].ToString();

分组的命名方式为:(?< groupname > exp) 捕获exp

到此这篇关于C# 获取文件夹里所有文件名的文章就介绍到这了,更多相关C# 获取文件夹文件名内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C# 获取文件夹里所有文件名的详细代码

本文链接: https://www.lsjlt.com/news/172336.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作