0

I need to keep few integer values recorded in a fixed location in disk to retrieve in case the application crashes. Is there an easier way to save few primitive data types without having to write/read to file or database?

madu
  • 5,232
  • 14
  • 56
  • 96
  • 3
    Have you seen [this](http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application) ? – ProgrammingLlama Apr 14 '17 at 04:57
  • 2
    What kind of application? – Anuradha Kulkarni Apr 14 '17 at 05:03
  • 1
    By "fixed location" do you mean a physical block like cylinder 42, surface 7, sector 3? Is there some reason that would be preferable to a file allocated in a more typical way by the file system in a logical volume? What, if any, file system are you trying to subvert? ([Defragmentation API](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363911(v=vs.85).aspx) and [FSCTL_MOVE_FILE control code](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364577(v=vs.85).aspx) for semi-explicit file placement.) – HABO Apr 14 '17 at 05:22
  • @HABO by "fixed location" I meant that a fixed location in the file system. Like the installation folder of the application. So I can assume that the application will always write/read from the same place. – madu Apr 14 '17 at 07:06

2 Answers2

2

If the number of stored parameters is constant, then you can simply save them as rows in a certain order with the help of filestream or fputs (fprintf) - to choose from. When reading sequentially, read lines, translate string values ​​to a variable of the desired type and then work with them. If the number of parameters is not exactly known, you can write the type of the parameter at the beginning of the line, and then its value. When reading, parse each line. Although, as it seems to me, the type of files ini is fixed, you can find and read it. But the principle of creating the same is to create a string of the desired content and write it to a file. Perhaps there is in the builder some standard component for working with ini-files.

1

You could use Environment Variables:

// Set a variable at user level
Environment.SetEnvironmentVariable("MyNumber", "1234", EnvironmentVariableTarget.User);

// Get a user variable
var x = Environment.GetEnvironmentVariable("MyNumber", EnvironmentVariableTarget.User);
thepirat000
  • 12,362
  • 4
  • 46
  • 72