且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用c#..写入特定位置的空文件?

更新时间:2023-10-09 13:07:10

你正在寻找Stream。寻求(..) [ ^ ] - 用于将虚拟光标放置在流中的方法(可能是FIL e)。



请注意,写一个流的中间不会插入你正在编写的内容,但会覆盖该位置的任何内容。没有简单的开关来改变它;如果你想插入,你必须通过阅读和重写来向后移动其余部分。



取决于你是哪个特定班级对于文件访问,你可能会发现Seek-method不是直接在该类对象上,而是在BaseStream属性下,例如: myStreamWriter.BaseStream.Seek(..)



还要注意,通常不允许通过您通常可以使用的文件获取搜索功能。您可以检查流是否允许使用其属性 .CanSeek 进行搜索。
You're looking for the Stream.Seek(..)[^]-method with which you can position your "virtual cursor" in a stream (which may be a file).

Be aware that writing "in the middle" of a stream will not insert whatever you're writing but it will overwrite whatever already is at that position. There's no simple switch to change this; if you'd want to insert, you'd have to move the remainder backwards yourself by reading and re-writing it.

Depending on which particular class you're using for file-access you might find the Seek-method not directly on that class-object but under the BaseStream-property, e.g.: myStreamWriter.BaseStream.Seek(..) .

Also be aware that the ability to seek is not generally allowed for a stream, with files you'll usually be able to though. You can check whether a stream allows seeking with its property .CanSeek .