C# 怎么弄一个 可以读写一个文件夹下的所有TXT文件

clly002 2011-11-12 11:16:52

C# 怎么弄一个 可以读写一个文件夹下的所有TXT文件

要涉及到哪些东西啊?

读写指定文件夹下的所有txt文件;
把所有的txt文件里面符合要求的字符串取出来,每个字符串一行;
保存到一个新的txt文件里。

这涉及到哪些东西 引用到哪些东西 类 方法?

希望指点一下 我good good Study
...全文
1417 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
MKing0412 2011-11-15
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20111115/00/b9314441-a805-4db6-9126-cf2676e8403c.html?61874
也有你想要的 别人的问题提得中就有你想要的,参考
clly002 2011-11-15
  • 打赏
  • 举报
回复

谢谢16楼
凭海听涛来 2011-11-15
  • 打赏
  • 举报
回复
remark!
标记一下,以前肯定用的到.
freemangood 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 caozhy 的回复:]
C# code

DirectoryInfo di = new DirectoryInfo(@"c:\path");
List<string> data = new List<string>();
foreach (var fi in di.GetFiles("*.txt", SearchOption.AllDirectories))
{
string txtcontent =……
[/Quote]

很多人给出答案了啊!
  • 打赏
  • 举报
回复
貌似都解决了啊。
绿领巾童鞋 2011-11-15
  • 打赏
  • 举报
回复
system.io;具体自己看
clly002 2011-11-15
  • 打赏
  • 举报
回复

结贴了 哈哈 19楼的哥们儿辛苦了
阿非 2011-11-15
  • 打赏
  • 举报
回复
Regex rgx = new Regex(@"http://www.XXXX.com/file/\d{10}/");

string path = @"H:\", newTxt = @"H:\newTxt.txt";

var query1 = from f in Directory.GetFiles(path)
let arr = File.ReadAllLines(f)
where f.EndsWith(".txt")
select new
{
content = (from c in arr
where rgx.IsMatch(c)
select c)
};


query1.ToList().ForEach(item => File.AppendAllLines(newTxt, item.content));

foreach (var str in File.ReadAllLines(newTxt))
{
Console.WriteLine(str);
}
jshi123 2011-11-14
  • 打赏
  • 举报
回复
where System.Text.RegularExpressions.Regex.IsMatch(c, @"http://www.XXXX.com/file/\d{10}/")
  • 打赏
  • 举报
回复
谢谢,收藏
clly002 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 sandy945 的回复:]

C# code

string path = @"H:\", newTxt = @"H:\newTxt.txt";

List<string> result = new List<string>();

var query1 = from f in Directory.GetFiles(path)
……
[/Quote]


这个一样都可以。。。

就是我在筛选条件那里 写成这样:where c.Contains("http://www.XXXX.com/file/\d{10}/")


也是提示 无法识别的转义序列。。。。 就是这个\d

。。。。


哎 不知道怎么在这里用正则了
clly002 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 caozhy 的回复:]

C# code
DirectoryInfo di = new DirectoryInfo(@"c:\path");
List<string> data = new List<string>();
foreach (var fi in di.GetFiles("*.txt", SearchOption.AllDirectories))
{
string txtcontent = "";
……
[/Quote]

。。 在条件那里 我写的是 where x=="http://www.XXXX.com/file/\d{10}/"

提示:无法识别的转义序列。。。。 就是这个\d
。。这里不知道怎么用正则来表示。。


风骑士之怒 2011-11-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 clly002 的回复:]
%>_<%

还有其他的吗。。。。。

这些关键字 我要一个一个的查。。。
[/Quote]

这些关键字 我要一个一个的查?

举个例子看看
threenewbee 2011-11-13
  • 打赏
  • 举报
回复
程序都给你了,贴进去就能用还不会的话,我很好奇,你写程序干吗?
clly002 2011-11-13
  • 打赏
  • 举报
回复

%>_<%

还有其他的吗。。。。。

这些关键字 我要一个一个的查。。。
Daqing 2011-11-13
  • 打赏
  • 举报
回复
/// <summary>
/// 获取路径下 所有的txt文件
/// </summary>
/// <param name="Dicname">路径</param>
/// <param name="extension">扩展名</param>
/// <returns>返回 (文件名 ,文件内容)的字典</returns>
Dictionary<string, string[]> dic = new Dictionary<string, string[]>();
private Dictionary<string, string[]> GetFolderTxtFile(string Dicname,string extension)
{
DirectoryInfo dinfo = new DirectoryInfo(Dicname);
foreach (FileInfo fname in dinfo.GetFiles())
{
if (fname is FileInfo & (Path.GetExtension(fname.FullName)).ToUpper().CompareTo(string.Concat(".", extension).ToUpper()) == 0)//遍历所有文件
{
//DateTime.Now.Millisecond.ToString()避免存在同样的文件名。
string filename=Path.GetFileNameWithoutExtension(fname.FullName);
if(dic.Keys.Contains(filename))
filename+=DateTime.Now.Millisecond.ToString();
dic.Add(filename, File.ReadAllLines(fname.FullName, Encoding.Default));
}
}
foreach (DirectoryInfo name in dinfo.GetDirectories())
{
if (name is DirectoryInfo)
//如果存在路径,则继续递归。
GetFolderTxtFile(name.FullName, extension);
}
return dic;
}

调用
FolderBrowserDialog fl = new FolderBrowserDialog();
fl.SelectedPath = @"F:\Ghost";
if (fl.ShowDialog() == DialogResult.OK)
GetFolderTxtFile(fl.SelectedPath,"txt");
阿非 2011-11-13
  • 打赏
  • 举报
回复
List<string> result = new List<string>();

可以去掉.
阿非 2011-11-13
  • 打赏
  • 举报
回复

string path = @"H:\", newTxt = @"H:\newTxt.txt";

List<string> result = new List<string>();

var query1 = from f in Directory.GetFiles(path)
let arr = File.ReadAllLines(f)
where f.EndsWith(".txt")
select new
{
content = (from c in arr
//筛选条件
where c.Contains("111")
select c)
};


query1.ToList().ForEach(item => File.AppendAllLines(newTxt, item.content));

foreach (var str in File.ReadAllLines(newTxt))
{
Console.WriteLine(str);
}
clly002 2011-11-13
  • 打赏
  • 举报
回复
谢谢各位
风骑士之怒 2011-11-12
  • 打赏
  • 举报
回复

namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string text = ReadTxt("D:\\test", SelectRule);
WriteTxt("D:\\a.txt", text);
Console.Read();
}

private static bool SelectRule(string text)
{
return text.Length > 5 ? true : false;
}

private static void WriteTxt(string path, string text)
{
using (StreamWriter sw = new StreamWriter(path))
{
sw.Write(text);
}
}

private static string ReadTxt(string path, Func<string, bool> selectRule)
{
StringBuilder text = new StringBuilder();
string temp = string.Empty;
DirectoryInfo folder = new DirectoryInfo(path);
FileInfo[] files = folder.GetFiles("*.txt", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
using (StreamReader sr = file.OpenText())
{
while (sr.Peek() > 0)
{
temp = sr.ReadLine();
if (selectRule(temp))
{
text.AppendLine(temp);
}
}
}
}
return text.ToString();
}
}
}


我这里是读取txt文件里每行内容长度大于5的内容,并汇总到一个txt文件里,

可以自己写个规则器,
加载更多回复(4)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧