app config not able to read in visual studio

Narvekar, Sharvari 11 Reputation points
2023-01-10T09:32:01.05+00:00

I made app.config file in Unit Test Folder and I wrote connection string and key value pairs in that file

<appSettings> <add key="ArchiveLocation" value="C:"/> </appSettings>

I wrote unit test case to check if the archive location exist. configurationValue = ConfigurationManager.AppSettings["ArchiveLocation"]

The test case is failing because the key and value is stored in app.config and not able to read that.

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,044 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Tianyu Sun-MSFT 30,351 Reputation points Microsoft Vendor
    2023-01-11T05:27:35.4633333+00:00

    Hello @Narvekar, Sharvari,

    Welcome to Microsoft Q&A forum.

    Based on my test, it should work. Just to confirm, you are testing this locally or using Azure/Azure DevOps? And your Unit Test project is based on .NET Framework not .NET/.NET Core right?

    You may need to confirm these things:

    1. The app.config/App.config file is included in the Unit Test project. If it’s not included in the Unit Test project, please click "Show All Files" button in VS Solution Explorer > find the displayed app.config/App.config file > right-click it > Include in Project > Clean, rebuild and run your test.
    2. Double check and confirm that the app.config/App.config file is stored in the Unit Test project folder, and make sure that the key was set in this file. Also please make sure that the <appSettings></appSettings> was set between <configuration> and </configuration>.
    3. If you are testing in Azure, please try to confirm that it works well locally.
    4. If you are using .NET Framework based project(Unit Test), and above suggestions don’t work, please try to create a new Unit Test project, and add a new app.config file, include it in the project. Add the <appSettings> into the app.config file and write the unit test case then run the test. If the same error appeared this time, please share the detailed steps with us to reproduce this issue and check further.

    BTW, are you using Visual Studio 2022?

    Feel free to contact us.

    Best Regards,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Madhavi Gudipati 0 Reputation points
    2023-04-24T17:46:03.0433333+00:00

    I am using VS 2022. Created app.config still i am not able to get the browser variable from app.settings. What am i doing wrong here?

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="ePortalUserName" value="madhavigudip"/>
        <add key="ePortalPassword" value ="PFCTrucks!1"/>
        <!--Possible Environment values - "Test", "Cert" -->
        <add key="Environment" value=""/>
        <add key="OLSPortalUrl" value="http://www-test.paccarfinancial.com/"/>
        <!-- Supported browser types are "IE", "Chrome" or "Firefox"  -->
        <add key="Browser" value="chrome"/>
        <!-- Max time (in seconds) to wait for page to load before throwing exception  -->
        <add key="PageLoadTimeout" value="25"/>
      </appSettings>
      <system.web>
        <membership defaultProvider="ClientAuthenticationMembershipProvider">
          <providers>
            <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri=""/>
          </providers>
        </membership>
        <roleManager defaultProvider="ClientRoleProvider" enabled="true">
          <providers>
            <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400"/>
          </providers>
        </roleManager>
      </system.web>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.0"/>
      </startup>
    </configuration>
    
     public void PAC041_01_Login()
            {
                string? browser = ConfigurationManager.AppSettings["Browser"];
                if (browser != null)
                {
                    //driver = Utility.InitBrowser(browser, pageLoadTimeout);
                    switch (browser.ToUpper())
                    {
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.