Search code examples
c#winformsembedded-resourceapplication-settings

A hidden settings file specific to an instance of an executable at a given path


Not sure I have the best title for this question. Feel free to modify it or suggest a change and I will do the edit myself.

I have a standalone executable which wants to maintain a settings.bin file specific to the application. There are two obvious ways to do it:

1. Create/Read the file from the local directory where the executable resides

  • Positive: User can copy the exe and bin file to multiple directories and have multiple versions with different settings.bin files.
  • Negative: I don't want to polute executable directory.

2. Create/Read the file from a "hidden" location like the Local Application Data folder.

  • Positive: Not poluting the executable directory.
  • Negative: The settings.bin file will be shared amoungst any instance of the executable regardless of where it is locaed.

I don't want to do either of these solutions because neither meet both of my requirements, which are:

  • Don't polute the executable directory (IE: don't create a local file).
  • The settings.bin file is different based on the location of the executable.

Any thoughts? I wanted to embed the settings.bin file as a resource but quickly learned you can't write to an embedded resource. I'm all out of ideas.


Solution

  • Use option 2 with a small modification that cancels your negative point (not shared)

    You must have something that you can differentiate between exe files.

    If all reside in different paths I would do the following:

    1) Hash the location of the executable (md5 of the path)

    2) Create a directory in the appdata with the hash

    3) Store my files there

    Else I would try to enumerate myself compared to other processes:

    1) When starting check the app data.

    2) Attempt to lock a file for writing at:[App Data]\1\sem.oi

    3) If that failed attempt to lock a file for writing at:[App Data]\2\sem.oi

    Use the settings in the directory that you were able to open the file in

    Hope this helps.