有一个文本文件,内容如下
46464646
4565464
45466546
现在我向读出每一行的数据(作为字符串)
怎么读阿?
谢谢

解决方案 »

  1.   

    配置ODBC"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\txt\;Extensions=txt;" 将查询结果放到DataSet中
      

  2.   

    using System;
    using System.IO;
    namespace Read
    {
    /// <summary>
    /// Security 的摘要说明。
    /// </summary>
    public class readFile
    {
    public static void Main(){ 
    System.IO.StreamReader objStreamReader;
    objStreamReader = System.IO.File.OpenText((@"c:\xc.txt"));
    string strScript =objStreamReader.ReadToEnd();
    objStreamReader.Close();
    Console.WriteLine(strScript);
    }
    }
    }
      

  3.   

    读取每一行,可以把上面ReadToEnd改成ReadLine
      

  4.   

    StreamReader sr = new StreamReader(yourFile);
    while (sr.Peek()>=0)
    {
        string sLine = sr.ReadLine();
        ....
    }
      

  5.   

    如果我只想读取第二行,而对其他几行不感兴趣,怎么办呢?另外,如果第二行是这样的:IPAddress=10.0.0.1.而我只对"10.0.0.1"感兴趣,又该怎么办呢?
      

  6.   

    呵呵,如果在你的文本文件中,肯定,ip是在第二行!
    那可用上面各位的方法把数据读出来(也就是ReadLine),赋给一个数组,然后再从数组中提取你需要的东西,这样弹性非常好!非常灵活!string= i[1];
      

  7.   

    嗯,如果对于固定格式的 txt文件,用ReadLine读成string〔〕,是个不错的办法,不过还要再做一次处理才能得到“10.0.0.1"这个关键的值。
    我这两天又找了找,发现C#有专门做这种事情的东东,叫做:“regular expression”
    ………………………………………………………………………………………………………………………Regular expressions are a concise and flexible notation for finding and replacing patterns of text. The regular expressions used within Visual Studio are a superset of the expressions used in Visual C++ 6.0, with a simplified syntax.           ---------MSDN
    ---------------------------------------------
    有兴趣的可以研究研究。
      

  8.   

    [Visual Basic]
    Imports System
    Imports System.IOClass Test
        Public Shared Sub Main()
            Try
                ' Create an instance of StreamReader to read from a file.
                Dim sr As StreamReader = New StreamReader("TestFile.txt")
                Dim line As String
                ' Read and display the lines from the file until the end 
                ' of the file is reached.
                Do
                    line = sr.ReadLine()
                    Console.WriteLine(Line)
                Loop Until line Is Nothing
                sr.Close()
            Catch E As Exception
                ' Let the user know what went wrong.
                Console.WriteLine("The file could not be read:")
                Console.WriteLine(E.Message)
            End Try
        End Sub
    End Class
    [C#]
    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            try 
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TestFile.txt")) 
                {
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null) 
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e) 
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
        }
    }
    下面的代码示例读取整个文件,并在检测到文件尾时发出通知。
    [Visual Basic]
    Option Explicit On 
    Option Strict On
    Imports System
    Imports System.IO
    Public Class TextFromFile
        Private Const FILE_NAME As String = "MyFile.txt"
        Public Shared Sub Main()
            If Not File.Exists(FILE_NAME) Then
                Console.WriteLine("{0} does not exist.", FILE_NAME)
                Return
            End If
            Dim sr As StreamReader = File.OpenText(FILE_NAME)
            Dim input As String
            input = sr.ReadLine()
            While Not input Is Nothing
                Console.WriteLine(input)
                input = sr.ReadLine()
            End While
            Console.WriteLine("The end of the stream has been reached.")
            sr.Close()
        End Sub
    End Class
    [C#]
    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;
            }
            StreamReader sr = File.OpenText(FILE_NAME);
            String input;
            while ((input=sr.ReadLine())!=null) 
            {
                Console.WriteLine(input);
            }
            Console.WriteLine ("The end of the stream has been reached.");
            sr.Close();
        }
    }
      

  9.   

    读文本文件用StreamReader
    using ( System.IO.StreamReader sr = new System.IO.StreamReader(@"c:\file.txt") )
    {
    string str1 = sr.ReadLine();//读第一行
    string str2 = sr.ReadLine();//读第二行
    ....//继续读其它的
    }//自动关闭文件,释放资源
      

  10.   

    处理IPAddress=10.0.0.1,用substring和indexof函数结合起来处理