一个读文件的示例:
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);
}
}
}
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);
}
}
}
解决方案 »
- 强类型的DataSet如何进行主从表的设置?
- winform中如何判断XML里面某个节点数值是否存在?
- 一个table怎么设一列 decimal保留二位小数
- 关于调用本地的应用程序打开服务器上问题
- http的GET方法
- ADO.NET中DataSet和Entity Framework的区别
- 有没有直接测试 字符串 string类型 是否是 整数 和 小数的 函数?
- SqlDataAdapter.Update () 参数不同的区别和性能?
- 非常Easy的问题...
- C#怎样访问数据据库?请给一个精简的例子连接SQL SERVER数据库!还有ADO.NET又是什么?怎样使用?
- C#.net连接MSSQL数据库和连接access2003数据库的区别?在线等(只有100分了,麻烦大家帮帮我!!)
- 关于将 object 数组 转换成具体数组类的问题。
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();
}
}