txt文档里的内容的格式如下:
标题1
aaaaaaaa
bbbbbbbb
cccccccc标题2
dddddddd
eeeeeeee
ffffffff标题3
gggggggg
hhhhhhhh
。。
用readline可以按行读出
现在文档内容其实是很多有标题的小段落组成,如何把每一段文字标题和内容分别读出存在不同数据库字段里
谢谢

解决方案 »

  1.   

      FileStream file = new FileStream(@"D:\Accounts.txt", FileMode.Open);            StreamReader reader = new StreamReader(file);
                bool isEnd = true;
                while (isEnd)
                {
                    string text = reader.ReadLine();
                    if (text == "")
                    {
                        //isnew
                        //标题
                           //e.g
                        //value="";
                    }
                    else
                    {
                        //TODO;
                        //e.g
                        //value=value+text;
                        //Array.add(value);
                    }
                    if (reader.EndOfStream)
                        isEnd = false;
                }
                file.Close();如果按照你说的格式的话,这样应该没什么问题。剩下的就看你怎么做了。
      

  2.   

    using System;
    using System.IO;public class TextFromFile 
    {
        private const string FILE_NAME = "MyFile.txt";    public static void Main(String[] args) 
        {
            if (!File.Exists(FILE_NAME)) 
            {
                Console.WriteLine("{0} does not exist.", FILE_NAME);
                return;
            }
            using (StreamReader sr = File.OpenText(FILE_NAME))
            {
                bool isTitle = true;
                String input;
                while ((input = sr.ReadLine()) != null) 
                {
                  if (string.IsNullOrEmpty(input))
                  {
                    isTitle = true;
                    continue;
                  }
                  if (isTitle)
                  {
                    // WriteTitleToDB(input);    // 是标题。
                    isTitle = false;
                  }
                  else
                  {
                    // WriteContentToDB(input);  // 是内容。
                  }
                }
            }
        }
    }
      

  3.   

    楼上的答案可以实现,不过要是读的是文本文件且还有中文,建议使用这个方式实例流对象,省得显示乱码
    StreamReader sr = new StreamReader(FILE_NAME,System.Text.Encoding.Default)
      

  4.   

    using System.IO;
    StreamReader sr = new StreamReader(FILE_Path,System.Text.Encoding.Default)
    string file=sr.ReadLine();然后可以把file可以读出来了
    System.Text.Encoding.Default这个是避免读出的是乱码
    这样可以按照格式读出中英文
    试试看吧
      

  5.   

    string file=sr.ReadLine(); 
    最好是   string file = sr.ReadToEnd();
      

  6.   

    MSDN里这方面都很多,楼上的兄弟都说了,LZ照着做就行了。
      

  7.   

            public string[] strPath()
            {
                StreamReader sr;
                try
                {
                    path = new string[5];
                    int i = 0;
                    string sLine = "";
                    sr = File.OpenText(Path.GetFileName("Path.ini"));
                    while (sLine != null)
                    {
                        sLine = sr.ReadLine();
                        if (sLine == null)
                            break;
                        path[i] = sLine;
                        i++;
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return path;
                }
                sr.Close();
                return path;
            }