开发者

Reading raw, unencoded content of file(bytes)

开发者 https://www.devze.com 2023-03-15 23:43 出处:网络
I have a mission to make a \"stamp creator\" for Powder Toy. The \"stamp\" looks like this: http://i.imgur.com/3Cac3.jpg . I\'d like to use the bytes\' values for my开发者_StackOverflow中文版 creator.

I have a mission to make a "stamp creator" for Powder Toy. The "stamp" looks like this: http://i.imgur.com/3Cac3.jpg . I'd like to use the bytes' values for my开发者_StackOverflow中文版 creator. For that I need to read the unencoded content of the file. Any help? Also, I'll need to create such content as well. (Maybe I'm better off using a byte[] array?(but how to enter the special control characters?))


You can easily use File.ReadAllBytes to read the file, and File.WriteAllBytes to write it back.

If you need more control instead of reading an entire file into memory, you can use File.OpenRead and File.OpenWrite to read and write using a Stream. If you do that make sure you dispose of it - using the using keyword is encouraged.

Here are a couple of examples

public void CopyFileByReadingItAllToMemory() {
    byte[] data = File.ReadAllBytes(@"C:\Temp\input");
    File.WriteAllBytes(@"C:\temp\output", data);
}

public void CopyFileWithoutReadingItAllToMemory() {
    using (Stream input = File.OpenRead(@"C:\Temp\input"))
    using (Stream output = File.OpenWrite(@"C:\Temp\output"))
    {
        while (true) {
            int value = input.ReadByte();
            if (value == -1) {
                break;
            }

            output.WriteByte((byte)value);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号