日期:2014-05-17  浏览次数:21063 次

求高人指教,我是C#初学者,想提取txt文件中的内容
具体问题:我在窗体中有一个按钮,我想按按钮实现用正则表达式从txt文件中读取所选择相关内容,比如从硬盘某一位置读取txt文件,文件中有名字如hello的字符串,可以读出在我另一个控件中显示,谢谢啦!!!

------解决方案--------------------
streamreader的ReadLine,ReadToEnd等
------解决方案--------------------
string str = System.IO.File.ReadAllText("D:\\1.txt");

------解决方案--------------------
C# code

//文件位置
 string path = this.txtFilePath.Text;
           
            if (string.IsNullOrEmpty(path))
            {
                MessageBox.Show("文件位置必须填写");
                return;
            }
               //创建读取流
               StreamReader sr = new StreamReader(path);
            try
            {
                显示的控件.Text = sr.ReadToEnd();
            }
            catch (IOException ex)
            {

                MessageBox.Show(ex.Message);
            }
           
            sr.Close();

------解决方案--------------------
C# code

//文件位置
 string path = this.txtFilePath.Text;
           
            if (string.IsNullOrEmpty(path))
            {
                MessageBox.Show("文件位置必须填写");
                return;
            }
               //创建读取流
               StreamReader sr = new StreamReader(path);
            try
            {
                显示的控件.Text = sr.ReadToEnd();
            }
            catch (IOException ex)
            {

                MessageBox.Show(ex.Message);
            }
           
            sr.Close();

------解决方案--------------------
C# code
string path = @"C:\\";
                DirectoryInfo dir = new DirectoryInfo(path);
                foreach (FileInfo file in dir.GetFiles(path))
                {
                    if (file.Extension.Equals("txt"))
                    {
                        string filePath = file.FullName;
                        string txtContent = File.ReadAllText(filePath, Encoding.GetEncoding("GB2312"));//读取文件
                    }
                }

------解决方案--------------------
探讨
那如果我想和txt内容中的某些字符串匹配呢?

------解决方案--------------------
C# code
 string path = @"C:\\";
                DirectoryInfo dir = new DirectoryInfo(path);
                foreach (FileInfo file in dir.GetFiles())
                {
                    if (file.Extension.Equals(".txt"))//如果是txt文件
                    {
                        string filePath = file.FullName;
                        string txtContent = File.ReadAllText(filePath, Encoding.GetEncoding("GB2312"));//读取文件
                        if (txtContent.Contains("hello"))
                        {
                            //如果文件内容中包含hello
                        }
                    }
                }

------解决方案--------------------
呵呵,这你算问对人了,我喜欢一行一行的读,代码清晰简单,然后自己转换,我把我的经验心得分给你吧!

if (File.Exists("F:\\图书1.txt"))
{
string[] str = File.ReadAllLines("F:\\图书1.txt");
}
这样一个TXT就存在一个STR字符串数组里了,str[0]是第一行数据,一次类推。

有人问这样的话一行一行我想让他们分开啊 不想这样比如说我想把数据导入到LISTVIEW里面 怎么办。嘿嘿 你又找对人啦!
for (int i = 1; i < str.Length; i++)
{
char[] ch = new char[]{' '};
string[] row = str[i].Split(ch,StringSplitOptions.RemoveEmptyEntries); 
listView1.Items.Add(new ListViewItem (row));
}