使用StreamWriter 写文件时,只能把文件全部内容读出来,修改后再全部写回去。
想要改变文件某个位置的值的话,下面方法可行,试了很久好不容易试出来的。
private void button1_Click(object sender, EventArgs e)
{
try
{
byte[] info = Encoding.GetEncoding("Shift_JIS").GetBytes("kkkk");
{
try
{
byte[] info = Encoding.GetEncoding("Shift_JIS").GetBytes("kkkk");
FileStream fs = new FileStream("D://c.txt", FileMode.OpenOrCreate);
fs.Seek(30, SeekOrigin.Begin);
fs.Write(info, 0, info.Length);
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
文件定位修改
本文介绍了一种使用FileStream和Seek方法直接定位到文件特定位置进行内容修改的技术。这种方法避免了需要读取整个文件内容再写回的繁琐过程,提高了文件编辑效率。
1万+

被折叠的 条评论
为什么被折叠?



