这篇文章主要介绍了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
到此这篇关于C# 获取文件夹里所有文件名的文章就介绍到这了,更多相关C# 获取文件夹文件名内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
织梦狗教程
本文标题为:C# 获取文件夹里所有文件名的详细代码


基础教程推荐
猜你喜欢
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- 实例详解C#实现http不同方法的请求 2022-12-26
- C# 解析XML和反序列化的示例 2023-04-14
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- Unity shader实现高斯模糊效果 2023-01-16
- C#中的Linq to JSON操作详解 2023-06-08
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30