C#:prepending到文件的开头 [英] C#: Prepending to beginning of a file

查看:478
本文介绍了C#:prepending到文件的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是用C#文本添加到文件的开头的最佳方式?

What is the best way to add text to the beginning of a file using C#?

我无法找到一个简单的方法来做到这一点,但一对夫妇的变通走了过来。

I couldn't find a straightforward way to do this, but came up with a couple work-arounds.


  1. 打开新的文件,写我想添加,从旧文件中的文本附加到新的文件末尾的文本。

  1. Open up new file, write the text that I wanted to add, append the text from the old file to the end of the new file.

由于我要添加应不超过200个字符,我在想,我可以空格字符添加到文件的开头,再覆盖以我想添加文本的空白文本

Since the text I want to add should be less than 200 characters, I was thinking that I could add white space characters to the beginning of the file, and then overwrite the white space with the text I wanted to add.

有没有其他人遇到这个问题,如果是这样,你做了什么?

Has anyone else come across this problem, and if so, what did you do?

推荐答案

添加到文件(而不是追加prepending)的开头一般是不支持的操作。你的#1选项是罚款。如果你不能写一个临时文件,您可以将您的数据拉动整个文件到内存中,pre preND的字节数组,然后覆盖它退了出去(这是唯一真正可行的,如果你的文件很小你不必在内存一堆一次,因为prepending数组不一定不容易复制其一)。

Adding to the beginning of a file (prepending as opposed to appending) is generally not a supported operation. Your #1 options is fine. If you can't write a temp file, you can pull the entire file into memory, preprend your data to the byte array and then overwrite it back out (this is only really feasible if your files are small and you don't have to have a bunch in memory at once because prepending the array is not necessarily easy without a copy either).

这篇关于C#:prepending到文件的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆