嗯,如果对于固定格式的 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 --------------------------------------------- 有兴趣的可以研究研究。
[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(); } }
读文本文件用StreamReader using ( System.IO.StreamReader sr = new System.IO.StreamReader(@"c:\file.txt") ) { string str1 = sr.ReadLine();//读第一行 string str2 = sr.ReadLine();//读第二行 ....//继续读其它的 }//自动关闭文件,释放资源
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);
}
}
}
while (sr.Peek()>=0)
{
string sLine = sr.ReadLine();
....
}
那可用上面各位的方法把数据读出来(也就是ReadLine),赋给一个数组,然后再从数组中提取你需要的东西,这样弹性非常好!非常灵活!string= i[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
---------------------------------------------
有兴趣的可以研究研究。
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();
}
}
using ( System.IO.StreamReader sr = new System.IO.StreamReader(@"c:\file.txt") )
{
string str1 = sr.ReadLine();//读第一行
string str2 = sr.ReadLine();//读第二行
....//继续读其它的
}//自动关闭文件,释放资源