Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

use appsettings.json in console app

All that’s required is to add the following NuGet packages and an appsettings.json file.

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json

The appsettings.json files “Copy to Output Directory” property should also be set to “Copy if newer” so that the application is able to access it when published.
Comment

create appsettings.json inno setup

[Code]

#include "JsonParser.pas"

function FindJsonValue(
  Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
  var Value: TJsonValue): Boolean;
var
  I: Integer;
begin
  for I := 0 to Length(Parent) - 1 do
  begin
    if Parent[I].Key = Key then
    begin
      Value := Parent[I].Value;
      Result := True;
      Exit;
    end;
  end;

  Result := False;
end;

function FindJsonArray(
  Output: TJsonParserOutput; Parent: TJsonObject; Key: TJsonString;
  var Arr: TJsonArray): Boolean;
var
  JsonValue: TJsonValue;
begin
  Result :=
    FindJsonValue(Output, Parent, Key, JsonValue) and
    (JsonValue.Kind = JVKArray);

  if Result then
  begin
    Arr := Output.Arrays[JsonValue.Index];
  end;
end;

{ ... }

var
  JsonLines: TStringList;
  JsonParser: TJsonParser;
  LaunchSettingsArray: TJsonArray;
  ToolDirectoryValue: TJsonValue;
  I: Integer;
begin
  { ... }

  JsonLines := TStringList.Create;
  JsonLines.LoadFromFile(FileName);

  ParseJson(JsonParser, JsonLines.Text);

  if Length(JsonParser.Output.Errors) > 0 then
  begin
    Log('Error parsing JSON');
    for I := 0 to Length(JsonParser.Output.Errors) - 1 do
    begin
      Log(JsonParser.Output.Errors[I]);
    end;
  end
    else
  begin
    if FindJsonArray(
         JsonParser.Output, JsonParser.Output.Objects[0],
         'launchSettings', LaunchSettingsArray) and
       (GetArrayLength(LaunchSettingsArray) >= 0) and
       (LaunchSettingsArray[0].Kind = JVKObject) and 
       FindJsonValue(
         JsonParser.Output,
         JsonParser.Output.Objects[LaunchSettingsArray[0].Index], 'toolDirectory',
         ToolDirectoryValue) and
       (ToolDirectoryValue.Kind = JVKString) then
    begin
      Log(Format(
        'launchSettings[0]:toolDirectory:%s', [
        JsonParser.Output.Strings[ToolDirectoryValue.Index]]));
      JsonParser.Output.Strings[ToolDirectoryValue.Index] := 'Test';
      JsonLines.Clear;
      PrintJsonParserOutput(JsonParser.Output, JsonLines);
      JsonLines.SaveToFile(FileName);
    end;
  end;

  ClearJsonParser(JsonParser);
  JsonLines.Free;
end;
Comment

appsettings in console application c#


<ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.3" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.3" />
</ItemGroup>

Comment

appsettings in console application c#


{
    "SomeKey": "This is from Config!"
}

Comment

PREVIOUS NEXT
Code Example
Javascript :: js add to map 
Javascript :: ckeditor get instance from textarea 
Javascript :: yup string date schema validation 
Javascript :: export component react 
Javascript :: prevent duplicate entries in javascript array 
Javascript :: how to set window location search without reload 
Javascript :: remove spaces from string javascript 
Javascript :: jquery if element appears 
Javascript :: let var diferencia 
Javascript :: window resize done 
Javascript :: Material-ui bank icon 
Javascript :: handlebarsjs each first element 
Javascript :: Javascript - convert string value to lowercase 
Javascript :: js logical operators 
Javascript :: Comparing and Filtering two arrays 
Javascript :: axios post request with authorization header and body 
Javascript :: ref in functional components 
Javascript :: js basic user class 
Javascript :: password regex 
Javascript :: jquery close 
Javascript :: javascript fill 2 dimensional array 
Javascript :: ngfor only x item 
Javascript :: react cdn link 
Javascript :: angular ng-click toggle class 
Javascript :: accessing objects inside array 
Javascript :: react form hook npm 
Javascript :: redux mapstatetoprops get props 
Javascript :: anonymous function javascript 
Javascript :: how to add options to select in jquery array 
Javascript :: Not Found The requested URL was not found on this server angular routing when going back to site from ecternal source 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =