软件编程
位置:首页>> 软件编程>> C#编程>> C# 获取文件夹里所有文件名的详细代码

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

作者:ou.cs  发布时间:2023-01-07 02:05:20 

标签:C#,获取,文件夹,文件名

主要是这个方法

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

来源:https://blog.csdn.net/weixin_44291381/article/details/124349994

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com