我有一个配置文件 (.cfg),用于创建命令行应用程序以将用户添加到 SFTP 服务器应用程序。
cfg 文件需要为 cfg 文件中的每个条目保留一定数量的保留字节。我目前只是通过创建字节数组并将其转换为字符串,然后将其复制到文件中,将新用户附加到文件末尾,但我遇到了障碍。配置文件需要在文件末尾有 4 个字节。
我需要完成的过程是从文件中删除这些尾随字节,附加新用户,然后将字节附加到末尾。
现在您已经了解了我的问题背后的一些背景。
这是问题:
如何从字节数组中删除和添加字节?
这是我到目前为止得到的代码,它从一个文件读取用户并将其附加到另一个文件。
static void Main(string[] args)
{
System.Text.ASCIIEncoding code = new System.Text.ASCIIEncoding(); //Encoding in ascii to pick up mad characters
StreamReader reader = new StreamReader("one_user.cfg", code, false, 1072);
string input = "";
input = reader.ReadToEnd();
//convert input string to bytes
byte[] byteArray = Encoding.ASCII.GetBytes(input);
MemoryStream stream = new MemoryStream(byteArray);
//Convert Stream to string
StreamReader byteReader = new StreamReader(stream);
String output = byteReader.ReadToEnd();
int len = System.Text.Encoding.ASCII.GetByteCount(output);
using …
Run Code Online (Sandbox Code Playgroud)