C# 怎么弄一个 可以读写一个文件夹下的所有TXT文件
C# 怎么弄一个 可以读写一个文件夹下的所有TXT文件
要涉及到哪些东西啊?
读写指定文件夹下的所有txt文件;
把所有的txt文件里面符合要求的字符串取出来,每个字符串一行;
保存到一个新的txt文件里。
这涉及到哪些东西 引用到哪些东西 类 方法?
希望指点一下 我good good Study --------------------编程问答-------------------- C#遍历指定文件夹中的所有文件
DirectoryInfo TheFolder=new DirectoryInfo(folderFullName);
//遍历文件夹
foreach(DirectoryInfo NextFolder in TheFolder.GetDirectories())
this.listBox1.Items.Add(NextFolder.Name);
//遍历文件
foreach(FileInfo NextFile in TheFolder.GetFiles())
this.listBox2.Items.Add(NextFile.Name);
===================================================================
如何获取指定目录包含的文件和子目录
1. DirectoryInfo.GetFiles():获取目录中(不包含子目录)的文件,返回类型为FileInfo[],支持通配符查找;
2. DirectoryInfo.GetDirectories():获取目录(不包含子目录)的子目录,返回类型为DirectoryInfo[],支持通配符查找;
3. DirectoryInfo. GetFileSystemInfos():获取指定目录下(不包含子目录)的文件和子目录,返回类型为FileSystemInfo[],支持通配符查找;
如何获取指定文件的基本信息;
FileInfo.Exists:获取指定文件是否存在;
FileInfo.Name,FileInfo.Extensioin:获取文件的名称和扩展名;
FileInfo.FullName:获取文件的全限定名称(完整路径);
FileInfo.Directory:获取文件所在目录,返回类型为DirectoryInfo;
FileInfo.DirectoryName:获取文件所在目录的路径(完整路径);
FileInfo.Length:获取文件的大小(字节数);
FileInfo.IsReadOnly:获取文件是否只读;
FileInfo.Attributes:获取或设置指定文件的属性,返回类型为FileAttributes枚举,可以是多个值的组合
FileInfo.CreationTime、FileInfo.LastAccessTime、FileInfo.LastWriteTime:分别用于获取文件的创建时间、访问时间、修改时间;
--------------------编程问答--------------------
谢谢楼上给我开了个头 让我知道怎么在指定文件夹下找文件
问题是只有一个指定的文件夹 文件夹里面的文件全部都是txt
实现
读取所有txt文件取出符合规则的字符串存到新的txt文件中
这里面涉及到哪些知识哇 --------------------编程问答-------------------- 读取内容后,可以用正则来匹配你想要的数据
Regex. --------------------编程问答--------------------
DirectoryInfo di = new DirectoryInfo(@"c:\path");--------------------编程问答--------------------
List<string> data = new List<string>();
foreach (var fi in di.GetFiles("*.txt", SearchOption.AllDirectories))
{
string txtcontent = "";
using (StreamReader sr = new StreamReader(fi.FullName))
{
txtcontent = sr.ReadToEnd();
}
data.AddRange(from x in txtcontent.Split(new char[] { '\r', '\n' }
where 在这里写你的筛选条件
select x);
}
using (StreamWriter sw = new StreamWriter("c:\\目标文件.txt"))
{
sw.Write(string.Join("\r\n", data));
}
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文件里,
可以自己写个规则器, --------------------编程问答-------------------- /// <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"); --------------------编程问答--------------------
%>_<%
还有其他的吗。。。。。
这些关键字 我要一个一个的查。。。 --------------------编程问答-------------------- 程序都给你了,贴进去就能用还不会的话,我很好奇,你写程序干吗? --------------------编程问答--------------------
这些关键字 我要一个一个的查?
举个例子看看 --------------------编程问答--------------------
--------------------编程问答-------------------- List<string> result = new List<string>();
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);
}
可以去掉. --------------------编程问答-------------------- 谢谢各位
--------------------编程问答--------------------
。。 在条件那里 我写的是 where x=="http://www.XXXX.com/file/\d{10}/"
提示:无法识别的转义序列。。。。 就是这个\d
。。这里不知道怎么用正则来表示。。
--------------------编程问答--------------------
这个一样都可以。。。
就是我在筛选条件那里 写成这样:where c.Contains("http://www.XXXX.com/file/\d{10}/")
也是提示 无法识别的转义序列。。。。 就是这个\d
。。。。
哎 不知道怎么在这里用正则了 --------------------编程问答-------------------- 谢谢,收藏 --------------------编程问答-------------------- where System.Text.RegularExpressions.Regex.IsMatch(c, @"http://www.XXXX.com/file/\d{10}/") --------------------编程问答--------------------
谢谢16楼 --------------------编程问答-------------------- http://topic.csdn.net/u/20111115/00/b9314441-a805-4db6-9126-cf2676e8403c.html?61874
也有你想要的 别人的问题提得中就有你想要的,参考 --------------------编程问答--------------------
Regex rgx = new Regex(@"http://www.XXXX.com/file/\d{10}/");--------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- system.io;具体自己看 --------------------编程问答-------------------- 貌似都解决了啊。 --------------------编程问答--------------------
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);
}
很多人给出答案了啊! --------------------编程问答-------------------- remark!
标记一下,以前肯定用的到. --------------------编程问答-------------------- --------------------编程问答--------------------
结贴了 哈哈 19楼的哥们儿辛苦了
补充:.NET技术 , C#