覆盖FileStream中的字符串

时间:2016-10-16 21:07:12

标签: c#

我的代码是:

string sMD5Hash = sb.ToString();

using(Stream stream = new FileStream(sFile, FileMode.OpenOrCreate))
{
    stream.Seek(5, SeekOrigin.Begin);
    stream.Write(Encoding.ASCII.GetBytes(sMD5Hash), 0, sMD5Hash.Length);
}

我想要做的是,寻求5并开始在那里写我的字符串。不经常写,因为它会推动下一个字节前进。但相反,我想覆盖字节。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

制作FileStream FileMode.OpenOrCreateFileAccess.ReadWrite

using (Stream stream = new FileStream(sFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    stream.Seek(5, SeekOrigin.Begin);
    stream.Write(Encoding.ASCII.GetBytes(sMD5Hash), 0, sMD5Hash.Length);
}