开发者

Systerm.Configuration.ConfigurationSettings does not contain a definition for appSettings

I am new to programming, trying to copy files from one location to another, trying to use app.config file in Visual Studio 2010.

The config file is

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="sourcePath" value="C:\Users\Public\TestFolder"/>
<add key="targetPath" value="C:\Users\Public\TestFolder\SubDir"/>
</appSettings>
</configuration>

The code to copy the file is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FileCopy
{
    class FileCopy
    {
        class SimpleFileCopy
        {
            static void Main()
            {
                string fileName = "test.txt";
                string sourcePath = System.Configuration.ConfigurationSettings.appSettings["sourcePath"];
                string targetPath = System.Configuration.ConfigurationSettings.appSettings["targetPath"];

                // Use Path class to manipulate file and directory paths.
                string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
                string destFile = System.IO.Path.Combine(targetPath, fileName);

                // To copy a folder's contents to a new location:
                // Create a new target folder, if necessary.
                if (!System.IO.Directory.Exists(targetPath))
                {
                    System.IO.Directory.CreateDirectory(targetPath);
                }

                // To copy a file to another location and 
                // overwrite the destination file if it already exists.
                System.IO.File.Copy(sourceFile, destFile, true);

                // To copy all the files in one directory to another directory.
                // Get the files in the source folder. (To recursively iterate through
                // all subfolders under the current directory, see
                // "How to: Iterate Through a Directory Tree.")
                // Note: Check fo开发者_JAVA技巧r target path was performed previously
               //       in this code example.
                if (System.IO.Directory.Exists(sourcePath))
                {
                    string[] files = System.IO.Directory.GetFiles(sourcePath);

                    // Copy the files and overwrite destination files if they already  exist.
                    foreach (string s in files)
                    {
                        // Use static Path methods to extract only the file name from the path.
                        fileName = System.IO.Path.GetFileName(s);
                        destFile = System.IO.Path.Combine(targetPath, fileName);
                        System.IO.File.Copy(s, destFile, true);
                    }
                }
                else
                {
                    Console.WriteLine("Source path does not exist!");
                }

                // Keep console window open in debug mode.
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();

            }
        }
    }
}

I get an error when I build the solution which says 'System.Configuration.ConfigurationSettings' does not contain a definition for 'appSettings'

What am I doing wrong? Thanks for the help.


You need to use the configuration manager:

System.Configuration.ConfigurationManager.AppSettings["sourcePath"]


Had the same problem with .Net 6.

The solution that solves my problem was that I installed the System.Configuration.ConfigurationManager package. (I forgot to install the package)

Then I added the following using statement:

using System.Configuration;

Now the problem is that ConfigurationManager thinks it belongs to 2 namespaces, i.e. the System.Configuration namespace and another one.

To solve this I forced it to belong to the System.COnfiguration namespace by the following code:

using namespc = System.Configuration;

namespc.ConfigurationManager.AppSettings["DBConnectionString"]!

To summarize it all.

Install NuGet package System.Configuration.ConfigurationManager

Name of my configuration file is App.Config

Content of App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="DBConnectionString" value="aa" />
        <add key="DBName" value="bb" />
        <add key="AuthKey" value="cc" />
    </appSettings>
</configuration>

File reading values, i.e. SettingsService.cs:

using namespc = System.Configuration;

namespace Ecommerce_server.Services.SetupOperation
{
    public interface ISettingsService
    {
        string MongoDBConnectionString { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }

        string DataBaseName { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }

        public string AuthKey { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }

    }

    public class SettingsService : ISettingsService
    {
        public string MongoDBConnectionString { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }

        public string DataBaseName { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }

        public string AuthKey { get => namespc.ConfigurationManager.AppSettings["DBConnectionString"]!; }

    }
}


ConfigurationManager is outdated, you need to use IConfiguration in the .NET Сore environment (IConfiguration is provided by .NET Core built-in dependency injection).

    private readonly IConfiguration config;

    public MyConstructor(IConfiguration config)
    {
        this.config = config;
    }
    public void DoSomethingFunction()
    {
        string settings1 = config["Setting1"];
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜