Save bytes to a file

Asked

Viewed 337 times

0

I am making an application that needs to read the bytes of a fixed file, I am being able to read clearly, however, it bothers me that the file needs to follow my application every time it starts. Actually there’s a way I can 'save' these bytes inside my app or something?

  • Welcome to SP. Post the code you have already done and show where the problem/difficulty is occurring.

  • Thanks for the welcome :D, the code I did is just pass the file to an array of bytes and vice versa (array of bytes -> *.exe), but as I explained, I want to break the dependency of the original file, I want to have it "hardcoded" in my code for me to build it from scratch when it’s convenient, I just can’t think of how to save this array of bytes to use in new instances of the application, you know how to do this?

  • Who has denied, by kindness, pronounce.

1 answer

3


Think about it, there is no miracle. You want to take data from a file and put its contents inside your code. In the end this data is still saved somewhere - in your sources or binaries. So, why care?

Add this file to the project, mark to always copy the file in builds and tap forward to the solution. Now, if you really want to do this:

Read the file data, create a buffer and then create this buffer as C code#.

var buffer = File.ReadAllBytes(caminhoDoArquivoBinario);
System.Diagnostics.Debugger.Break(); // breakpoint após a leitura do buffer

Go then on Imediate Window and make:

buffer <ENTER>

Copy what was generated and paste into a file .cs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.