c#流编写器,,,,,,,,,,,,,,,,,,, 、、、、、、 [英] c# stream writer ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
问题描述
大家好,
我正在尝试从.SYS文件读取和写入二进制数据.我正在以二进制文件的形式对其进行访问.我应该在特定位置进行替换(例如:位置4至35).在读取特定数据时我使用了FileStream ,它的工作正常.在编写时,我有一个问题,我再次使用filestream,是否有任何功能只能在选定的位置进行替换.帮帮我.
在此先感谢
--- M.ARUN
Hi all ,
I am trying to read and write binary data from a .SYS file.I am accessing it as a binary file.I am supposed to replace at a specific position ( say : postion 4 to 35).while reading the particular data i used FileStream , its working fine.while writing , i have a problem ,, i am using filestream again ,, is there any function to replace only at a selected place. help me out.
thanks in advance
--- M.ARUN
推荐答案
您可以使用Position属性或Seek函数.
例如:
You can use Position property or Seek function.
For example:
System.IO.FileStream fs = new System.IO.FileStream(handle, System.IO.FileAccess.ReadWrite);
fs.Position = 35;
or
fs.Seek(35, System.IO.SeekOrigin.Current);
希望对您有所帮助.
I hope it helps.
使用System.IO名称空间中的BinaryWriter
,该名称空间具有功能Seek()
.使用它可以选择一个位置,然后使用Write()
函数来编写变量类型.
UseBinaryWriter
from System.IO namespace which has a functionSeek()
. Using that you can select a position and then use theWrite()
function to write your variable type.
将文件中的数据读取到字节数组中.更改您想要的字节.写回文件.
我不会为此烦恼!
Read the data from the file into a byte array. Change the bytes you want to. Write the file back.
I wouldn''t bother with a stream for this!
byte[] data = File.ReadAllBytes(@"F:\Temp\MyFile.sys");
data[4] = 0;
data[5] = 1;
...
File.WriteAllBytes(@"F:\Temp\MyFile.sys", data);
这篇关于c#流编写器,,,,,,,,,,,,,,,,,,, 、、、、、、的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!