0

I have already attached all the DLL files to the EXE file using Costura, but what about JSON now, because this is one of the main files in my application, it is responsible for the settings inside the application, and I would really not want any user to touch them.

UPD:

I added the settings file.json just as you say, added it to the resources and set everything as it should be, but how do I use it now? Here is the main question, I tried to do so:

        string path = TestProject.Properties.Resources.settings;
        using (StreamReader sw = new StreamReader(path))
        {
            string json = sw.ReadToEnd();
            cis = JsonConvert.DeserializeObject<ClientInfoSave>(json);
            ClientInfo.version_project = cis.version_project;
        }

And I get an error: Cannot implicitly convert byte type to string What am I doing wrong? I do not understand, please tell me how to fix it..

L1onelY
  • 1
  • 1
  • Create an embedded resource: https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – Mike67 Aug 01 '20 at 18:55
  • Change the build action of the file https://learn.microsoft.com/en-us/visualstudio/ide/build-actions?view=vs-2019 I believe changing it into `Embedded Resource` will work – Alvin Stefanus Aug 02 '20 at 10:50
  • @AlvinStefanus I did as you said. And there were errors... Error: `Couldn't find part of the path` Code: `using (StreamReader sw = new StreamReader(TestProject.Properties.Resources.settings + "/settings.json")) { string json = sw.ReadToEnd(); cis = JsonConvert.DeserializeObject(json); ClientInfo.version_project = cis.version_project; }` – L1onelY Aug 02 '20 at 11:19
  • You do not want to load the stream from a string path, you need to get it from the assembly `Assembly.GetExecutingAssembly()`, see the example from @Mike67 link. – Alvin Stefanus Aug 02 '20 at 11:39
  • @AlvinStefanus I updated my question by adding code, please tell me what can be done here? – L1onelY Aug 02 '20 at 12:53
  • Which line gives the conversion error? Also write the json string to a new text file to confirm it's correct. – Mike67 Aug 02 '20 at 13:58

1 Answers1

0
var assembly = Assembly.GetExecutingAssembly();

//This should be your json file name
var resourceName = "TestProject.Properties.Resources.settings.json";

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}

I do not know Costura, I only use the default compiler from Visual Studio, you can try to debug, check if var assembly contains your json file.


After I debug your project. These are the code to extract the data from your json file.

var assembly = Assembly.GetExecutingAssembly();
IEnumerable<TypeInfo> t = assembly.DefinedTypes;
TypeInfo tt = t.Where(m => m.Name == "Resources").FirstOrDefault();
PropertyInfo pi = tt.GetDeclaredProperty("project_settings");
byte[] mi = (byte[])pi.GetValue(assembly);
string results = System.Text.Encoding.UTF8.GetString(mi);

I got this in the results:

{"version_project":"0.0.1","activ_version_launcher_site":null,"update_launcher":false}

Alvin Stefanus
  • 1,873
  • 2
  • 22
  • 60
  • Error: `using (StreamReader reader = new StreamReader(stream))` `System.ArgumentNullException: "the Value cannot be undefined. Parameter name: stream"` – L1onelY Aug 02 '20 at 13:08
  • Make sure the `resourceName` is correct, I do not know the exact name that you use for the namespace and the assembly. – Alvin Stefanus Aug 02 '20 at 13:09
  • As you wrote, everything is as it is, and the path is identical. – L1onelY Aug 02 '20 at 13:18
  • Check with a debugger wheter the name is the same or not, because who knows the name is different (including upper and lower cases). Also you need to know if the json file is included in the assembly. – Alvin Stefanus Aug 02 '20 at 13:20
  • I did a test project, and I did all the things that you told me about, and I still can't get the final result, could you look at my test project, and tell me what I'm doing wrong? [Link to the test project](https://drive.google.com/file/d/1DsynO61A1bZkEpiMjCV_oQ02rKuW6bgb/view?usp=sharing) – L1onelY Aug 02 '20 at 13:41
  • Is this the json file content? {"version_project":"0.0.1","activ_version_launcher_site":null,"update_launcher":false} – Alvin Stefanus Aug 02 '20 at 14:28
  • Yes, but this is just the beginning, there will be a lot of settings – L1onelY Aug 02 '20 at 15:16
  • You can create a function to extract it, just pass the name of the setting file into the function and get it. Or combine all of the settings together in 1 file – Alvin Stefanus Aug 02 '20 at 15:18
  • How do I save anything to this file now? – L1onelY Aug 02 '20 at 15:20
  • You cannot, you cannot modify the assembly files when running, if I were you, I will use probably `localDB` to store the setting. https://learn.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-express-localdb?view=sql-server-ver15 – Alvin Stefanus Aug 02 '20 at 15:23