none
App reading settings from the wrong place RRS feed

  • Question

  • When my application is deployed, my application is supposed to read and write to appsettings.json and HMTokens.json files. The path is set in the code as:

                try
                {
                    hMTokens = JsonConvert.DeserializeObject<HMRCTokens>(File.ReadAllText(@".\\HMTokens.json"));
                    bTokensLoaded = true;
                }
                catch(Exception ex)
                {
                    WriteToOutputBox($"{ex.Message}");
    
                }
    
                try
                {
                    appSettings = JsonConvert.DeserializeObject<AppSettings>(File.ReadAllText(@".\\appsettings.json"));
                }
                catch (Exception ex)
                {
                    WriteToOutputBox($"{ex.Message}");
    
                }

    When the application is deployed I get these messages

    Could not find file 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Sage\Sage 300 2018\appsettings.json'.
    Could not find file 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Sage\Sage 300 2018\HMTokens.json'.

    What is the correct way to rad a file from the applications directory? Why is what I am doing incorrect?



    • Edited by MrSnert Thursday, March 14, 2019 9:10 AM
    Thursday, March 14, 2019 9:09 AM

Answers

  • If the file is in the same directory as the executable use the following.

    string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by MrSnert Thursday, March 14, 2019 12:05 PM
    Thursday, March 14, 2019 10:30 AM
    Moderator
  • Great, also you might consider even if this works to always assert if the file exists.

    string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
    if (File.Exists(fileName))
    {
        // work with file
    }
    else
    {
        // avoid run time exception, decide how to handle
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by MrSnert Wednesday, April 10, 2019 8:52 AM
    Thursday, March 14, 2019 11:36 AM
    Moderator

All replies

  • If the file is in the same directory as the executable use the following.

    string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by MrSnert Thursday, March 14, 2019 12:05 PM
    Thursday, March 14, 2019 10:30 AM
    Moderator
  • Thanks. I will test that

    Thursday, March 14, 2019 11:16 AM
  • Great, also you might consider even if this works to always assert if the file exists.

    string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
    if (File.Exists(fileName))
    {
        // work with file
    }
    else
    {
        // avoid run time exception, decide how to handle
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by MrSnert Wednesday, April 10, 2019 8:52 AM
    Thursday, March 14, 2019 11:36 AM
    Moderator