gpt4 book ai didi

c# - 在 C# 中读取流

转载 作者:行者123 更新时间:2023-12-02 00:41:41 26 4
gpt4 key购买 nike

我有这段代码用于从 Stream 中读取:

OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
Stream stream = File.Open(op.FileName,FileMode.Open);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, 10); // Problem is in this line
MessageBox.Show(System.Text.Encoding.UTF8.GetString(bytes));

此代码有效,但如果我更改标记行中的零,则 MessageBox 不会显示任何内容。

我该如何解决这个问题?

最佳答案

这是你要阅读的部分的起始索引,如果你改变它,你不会从头开始读取文件。

要从文件中读取内容,这是我最喜欢的方法:

using (var op = new OpenFileDialog())
{
if (op.ShowDialog() != DialogResult.OK)
return;
using (var stream = System.IO.File.OpenRead(op.FileName))
using (var reader = new StreamReader(stream))
{
MessageBox.Show(reader.ReadToEnd());
}
}

关于c# - 在 C# 中读取流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com