我有一个文本文件,文件内的内容如下:
XXXXXXXXXX
YYYYYYYYYY
ZZZZZZZZZZ
WWWWWWWWWW
现在我要实现通过C#读取此文本文件,然后分别把1-4行的值赋值给a,b,c,d4个变量,该怎么写?
同样,再把读出来的值返回去按对应行写入又该如何写?
因为小弟刚开始接触C#,很多东西不会,比如这个问题我就不知道到底该去什么地方查相关的例子,拜托各位再给我提供个可以学习的地方,最好是带例程的那种,谢谢

解决方案 »

  1.   

    Public Shared Sub Main()
            Dim path As String = "c:\temp\MyTest.txt"        Try
                If File.Exists(path) Then
                    File.Delete(path)
                End If            Dim sw As StreamWriter = New StreamWriter(path) '先写点内容
                sw.WriteLine("This")
                sw.WriteLine("is some text")
                sw.WriteLine("to test")
                sw.WriteLine("Reading")
                sw.Close()            Dim sr As StreamReader = New StreamReader(path) //开始读            Do While sr.Peek() >= 0
                    Console.WriteLine(sr.ReadLine()) //每行每行的读,然后显示出来
                Loop
                sr.Close()
            Catch e As Exception
                Console.WriteLine("The process failed: {0}", e.ToString())
            End Try
        End Sub
      

  2.   

    readline()我看到了
    我现在是想搞清楚如何把每行的值赋值给不同的变量?
      

  3.   

    int i;
    i=1;            While sr.Peek() >= 0
                {
                   switch(i)
                   {
                    case 1:
                         a= sr.ReadLine(); //每行每行的读,然后显示出来
                    break;
                    case 2:
                         b= sr.ReadLine();//每行每行的读,然后显示出来
                    break;
                    case 3:
                         c= sr.ReadLine(); //每行每行的读,然后显示出来
                    break;
                    case 4:
                         d=sr.ReadLine(); //每行每行的读,然后显示出来
                    break;
                    default: Console.WriteLine("over");
                    }
                }
      

  4.   

    int i;
    i=1;            While sr.Peek() >= 0
                {
                   switch(i)
                   {
                    case 1:
                         a= sr.ReadLine(); //每行每行的读,然后显示出来
                    break;
                    case 2:
                         b= sr.ReadLine();//每行每行的读,然后显示出来
                    break;
                    case 3:
                         c= sr.ReadLine(); //每行每行的读,然后显示出来
                    break;
                    case 4:
                         d=sr.ReadLine(); //每行每行的读,然后显示出来
                    break;
                    default: Console.WriteLine("over");
                    }
                    i=i+1;  //上面的这里写掉了,如果你是数据,更方便,直接用循环了
                }
      

  5.   

    public static string ReadTxt()
    {
    StreamReader inStr = null;
    string str = string.Empty;
    FileInfo textFile = new FileInfo(@"C:\test.txt");
    inStr = textFile.OpenText();
    //Console.WriteLine("\n Reading from text file: \n");
    string textLine = inStr.ReadLine();
    while(textLine != null)
    {
    str = str + "\n\r" + textLine;
    textLine = inStr.ReadLine();
    }
    inStr.Close();

    return str;
    }
      

  6.   

    int i = 0;
    string[] RD;
    while (sr.Peek() > 0)
    {
        rd[i] = sr.readline();
        i++;
    }
    string sn = Console.ReadLine();
    Console.Write(RD[int.Parse (sn)]);
      

  7.   

    http://www.syncfusion.com/FAQ/WindowsForms
    codeproject
    或《c#入门经典》都不错
      

  8.   

    copico(一路向北) 的例子不错呀