首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从C#应用程序打开txt文件

从C#应用程序打开txt文件的方法如下:

  1. 使用FileStream和StreamReader类
代码语言:csharp
复制
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);
                }
            }
        }
    }
}
  1. 使用File类
代码语言:csharp
复制
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);
        }
    }
}
  1. 使用ReadLines方法
代码语言:csharp
复制
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文件,并且可以按行读取文件内容。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

48秒

文件夹变成应用程序了并且无法打开的恢复视频

领券