你如何删除从C#中的字节数组添加字节字节、数组

2023-09-03 06:57:04 作者:③分玩笑ペ㈦分真

我有一个配置文件(.cfg),我使用创建一个命令行应用程序将用户添加到SFTP服务器应用程序。

CFG文件需要有一定数量的保留字节在CFG文件的每个条目。我目前在追加一个新的用户到该文件的末尾创建一个字节数组,并将其转换为一个字符串,然后将其复制到该文件,但我已经遇到了障碍。配置文件需要4个字节的文件的末尾。

我要完成这个过程是从文件中删除这些尾随字节,追加新的用户,然后附加字节到最后。

所以,现在你后面有我的问题的一些方面。

下面的问题:

如何删除,并从一个字节数组中添加字节?

tcp模拟将一个字节数组分2次传送到客户端,但是第二次传送的字节数组 服务端读到的是0 为什么解决必采纳

下面是code我有这么远,它从一个文件中读取用户和追加到另一个。

 静态无效的主要(字串[] args)
        {
            System.Text.ASCIIEncoding code =新System.Text.ASCIIEncoding(); //在ASCII编码拿起疯狂的字符
            StreamReader的读者=新的StreamReader(one_user.cfg,code,假,1072);

            字符串输入=;
            输入= reader.ReadToEnd();

            //转换输入字符串字节
            字节[]的字节数组= Encoding.ASCII.GetBytes(输入);
            MemoryStream的流=新的MemoryStream(字节阵列);

            //流转换为字符串
            StreamReader的byteReader =新的StreamReader(流);
            字符串输出= byteReader.ReadToEnd();
            INT的len = System.Text.Encoding.ASCII.GetByteCount(输出);

            使用(StreamWriter的作家=新的StreamWriter(freeFTPdservice.cfg,真实,Encoding.ASCII,5504))
            {
                writer.Write(输出,真正的);
                writer.Close();
            }

            Console.WriteLine(追加+ LEN);
            到Console.ReadLine();
            reader.Close();
            byteReader.Close();
        }
 

要尝试并说明这一点,这里是一个图。

  

1)加入第一用户

     在结束

文件(附加文本)字节(零)

     

2)添加第二个用户

     

文件(附加文本)(附文)字节结束(零)

     

等等。

解决方案

您不知道。可以拷贝到所需尺寸的新的数组。字节>或者你可以用名单,其中工作。键,然后从创建数组

不过,你的情况,我会建议寻找到文件流本身......他们让你读,写各字节或字节数组,也:

寻求

,它可以让你移动到文件中任意位置......所以,你所描述的用例,你会

打开文件(读/写访问) 将到文件的末尾 将回四个字节(你知道哪些他们是谁?如果不是,这将是一个很好的时间来隐藏他们) 写入新的用户 写四个字节 关闭文件

事情是这样的:

 使用(变种FS =新的FileStream(PATH,FileMode.Open,FileAccess.ReadWrite))
{
    fs.Seek(-4,SeekOrigin.End);
    fs.Write(userBytes);
    fs.Write(fourBytesAtEnd);
}
 

这还没有到思乐普整个文件,并把它写回退的优势。

I have a configuration file (.cfg) that I am using to create a command line application to add users to a SFTP server application.

The cfg file needs to have a certain number of reserved bytes for each entry in the cfg file. I am currently just appending a new user to the end of the file by creating a byte array and converting it to a string, then copying it to the file, but i've hit a snag. The config file requires 4 bytes at the end of the file.

The process I need to accomplish is to remove these trailing bytes from the file, append the new user, then append the bytes to the end.

So, now that you have some context behind my problem.

Here is the question:

How do you remove and add bytes from a byte array?

Here is the code I've got so far, it reads the user from one file and appends it to another.

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 (StreamWriter writer = new StreamWriter("freeFTPdservice.cfg", true, Encoding.ASCII, 5504))
            {
                writer.Write(output, true);
                writer.Close();
            }

            Console.WriteLine("Appended: " + len);
            Console.ReadLine();
            reader.Close();
            byteReader.Close();
        }

To try and illustrate this point, here is a "diagram".

1) Add first user

File(appended text)Bytes at end (zeros)

2) Add second user

File(appended text)(appended text)bytes at end (zeros)

and so on.

解决方案

You don't. You can copy to a new array of the desired size. Or you can work with a List<byte> and then create an array from that.

But, in your case, I would suggest looking into the file streams themselves... they let you read and write individual bytes or byte arrays and also:

Seek

which lets you move around to arbitrary locations in the file... So, for the use case you described, you would

open the file (for read/write access) move to the end of the file move back four bytes (do you know which ones they are? if not, this would be a good time to stash them) write the new user write the four bytes close the file

Something like this:

using (var fs = new FileStream(PATH, FileMode.Open, FileAccess.ReadWrite))
{
    fs.Seek(-4, SeekOrigin.End);
    fs.Write(userBytes);
    fs.Write(fourBytesAtEnd);      
}

This also has the advantage of not having to slurp in the whole file and write it back out.