从C#应用程序打开txt文件的方法如下:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = @"C:\example.txt";
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
}
using System;
using System.IO;
class Program
{
static void Main()
{
string path = @"C:\example.txt";
string[] lines = File.ReadAllLines(path);
foreach (string line in lines)
{
Console.WriteLine(line);
}
}
}
using System;
using System.IO;
using System.Collections.Generic;
class Program
{
static void Main()
{
string path = @"C:\example.txt";
foreach (string line in File.ReadLines(path))
{
Console.WriteLine(line);
}
}
}
以上三种方法都可以从C#应用程序中打开txt文件,并且可以按行读取文件内容。
领取专属 10元无门槛券
手把手带您无忧上云