I am a maker in programming, so I ask the question how best to do it: you need to select a file on the computer, perform some actions with it (for example, encrypt it), and then save it. The only thing I did was open the file via OpenFileDialog (at the touch of a button). Please do not throw slippers, if I ask the simplest questions - I do not understand)

  • Is the file text or binary? - Sergey
  • @ Sergey, initially assumes that you need to work with a file of any format, i.e. yes, then I will work with binary - AliciaWulf
  • I will not answer, probably, but there are several ways to work with files. One of them you have already indicated. But it is also possible through the File class, for example, like this: byte[] content = File.ReadAllBytes(<Путь к файлу>) . The File class has other methods . In general, it would not be bad to read about working with files in principle. Information - darkness. Here is just one example. - BlackWitcher
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Suppose you got the name of the file to read it.

 OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) { string filename = openFileDialog.FileName; } 

Next, there are options. For example, you can read the contents in a MemoryStream to work with it in RAM:

 using (MemoryStream memoryStream = new MemoryStream(File.ReadAllBytes(filename))) { // Делаем что-то важное } 

Then after the necessary transformations save it:

 using (FileStream fileStream = new FileStream("Какое_то_имя_файла.расширение", FileMode.Create, FileAccess.Write)) { byte[] bytes = new byte[memoryStream.Length]; memoryStream.Read(bytes, 0, (int)memoryStream.Length); fileStream.Write(bytes, 0, bytes.Length); } 
  • one
    And when you vote against it, can you tell me what is wrong? - Vadim Ovchinnikov