C# 读取文件

王林
发布: 2024-09-03 15:05:22
原创
765 人浏览过

执行文件操作是程序员生活中不可或缺的一部分,所有编程语言都提供各种库或函数来实现相同的目的。在 C# 中,可以使用 File 类提供程序中提供的方法完成相同的操作。一般从文件读取是使用ReadAllText(file)和ReadAllLines(file)两个方法,其中file表示需要读取的文件。还可以使用 Streamreader 以字节形式读取文件。本文将详细介绍 C# 中可用于读取文件的各种方法以及适当的示例。

语法:

ReadAllText() 具有以下语法

public static string ReadAllText (String Path, System.Text.Encoding encoding)
登录后复制

ReadAllLines() 具有以下语法

public static string ReadAllLines(String, Encoding)
登录后复制

此方法读取文件中存在的所有行,然后将它们存储在字符串中,然后关闭文件。

C#读取文件的参数

  • 路径:路径包含文件的位置。必须阅读此文件。
  • 编码:表示文件的编码类型,可选。

该方法的返回类型是一个字符串,包含文件中的所有内容。此方法在 System.IO 命名空间中可用,与此方法关联的程序集是 mscorlib.dll。

与 ReadAllLines() 方法的 ReadAllText() 相关的异常:

  • 参数异常:当路径有零个字符、空格或无效字符时会发生这种情况。
  • 参数空异常:当路径为空时会发生这种情况。
  • 路径太长异常:当路径超出实际系统定义的限制时会发生这种情况。
  • IO 异常:打开文件时如果发生任何错误,就会出现此异常。
  • 未经授权的访问异常:当指定文件为只读时会发生这种情况
  • 找不到文件异常:当文件不在指定位置时会发生这种情况。
  • 不支持的异常:当指定的路径格式无效时,抛出此异常。
  • 安全异常:当用户无权访问该文件时,会抛出此异常。

C# 读取文件的示例

以下是下面提到的以下示例。

示例#1 – 使用 ReadAllText() 读取文件

代码:

using System;
using System.IO;
using System.Text;
namespace ReadAllText
{
class Test
{
static void Main(string[] args)
{
var Fpath= @"C:\Vignesh\KB.txt";
string content = File.ReadAllText(Fpath, Encoding.UTF8);
Console.WriteLine(content);
}
}
}
登录后复制

输出:

C# 读取文件

示例 #2 – 使用 ReadAllLines() 读取文件

代码:

using System;
using System.IO;
using System.Text;
namespace ReadAllLines
{
class Test
{
static void Main(string[] args)
{
var inputfile = @"C:\Vignesh\append.txt";
string[] output = File.ReadAllLines(inputfile, Encoding.UTF8);
foreach (string op in output)
{
Console.WriteLine(op);
}
}
}
}
登录后复制

输出:

C# 读取文件

示例 #3 – 使用 StreamReader 类读取文件

1。 StreamReader.ReadToEnd(): 该方法用于从当前位置读取文件到流末尾。该方法对应的命名空间是System.Io,程序集是mscorblib.dll。

语法:

public override string ReadToEnd ();
登录后复制

输入参数:此方法不需要任何输入参数。

返回:此方法将文件内容作为流输出,如果当前位置设置为文件的最后一个字符,则返回空字符串。

2。 StreamReader.ReadLine(): 此方法从当前流中读取字符并将数据作为字符串发送到输出。该方法对应的命名空间是System.Io,程序集是mscorblib.dll。

语法:

public override string ReadLine();
登录后复制

输入参数:此方法不需要任何输入参数。

返回:返回当前流的下一行,如果当前流位于最后一行位置则返回 null。

代码:

using System;
using System.IO;
using System.Text;
class Program
{
static void Main(string[] args)
{
var FP = @"C:\Vignesh\Names.txt";
using var fstre = new FileStream(FP, FileMode.Open, FileAccess.Read);
using var sree = new StreamReader(fstre, Encoding.UTF8);
string Fcontent = sree.ReadToEnd();
Console.WriteLine(Fcontent);
}
}
登录后复制

输出:

C# 读取文件

代码:

using System;
using System.IO;
using System.Text;
class Program
{
static void Main(string[] args)
{
var filpath = @"C:\Vignesh\TimerJob-2019-08-09.txt";
using var fstre = new FileStream(filpath, FileMode.Open, FileAccess.Read);
using var sreee = new StreamReader(fstre, Encoding.UTF8);
string cline = String.Empty;
while ((cline = sreee.ReadLine()) != null)
{
Console.WriteLine(cline);
}
}
}
登录后复制

输出:

C# 读取文件

代码:

using System;
using System.IO;
namespace testclass {
class Test {
string FPath = @ "C:\Vignesh\Script to 0365 connection.txt";
static void Main(string[] args)
{
//Check if file is there at the path
//ReadallOutput()
if (File.Exists(FPath)) {
string output = File.ReadAlloutput(FPath);
Console.WriteLine(output);
}
//Check if file is there at the path
if (File.Exists(FPath)) {
//ReadallLines()
string[] Flines = File.ReadAllFlines(FPath);
foreach(string line in Flines)
Console.WriteLine(line);
}
//Check if file is there at the path
if (File.Exists(FPath)) {
//using streamreader
using(StreamReader file = new StreamReader(FPath)) {
int counter = 0;
string lgth;
while ((lgth = file.ReadLine()) != null) {
Console.WriteLine(lgth);
counter++;
}
file.Close();
}
}
Console.ReadKey();
}
}
}
登录后复制

输出:

C# 读取文件

示例 #4 – 使用 StreamReader 异步读取文件

代码:

using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
class TestProgram
{
static async Task Main(string[] args)
{
var ip = @" C:\Vignesh\Patching\Patching Steps.txt";
using var fssss = new FileStream(ip, FileMode.Open, FileAccess.Read);
using var srrr = new StreamReader(fssss, Encoding.UTF8);
//Reading asynchronously
string op = await srrr.ReadToEndAsync();
Console.WriteLine(op);
}
}
登录后复制

输出:

C# 读取文件

结论

因此,本文详细介绍了 C# 中的读取文件功能。它解释了可用于执行该操作的各种方法。它还涵盖了与每种方法相关的各种参数和异常,并结合示例程序的示例进行了详细解释。为了更详细地介绍,建议编写示例程序并进行练习。

以上是C# 读取文件的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!