【问题标题】:Systerm.Configuration.ConfigurationSettings does not contain a definition for appSettingsSysterm.Configuration.ConfigurationSettings 不包含 appSettings 的定义
【发布时间】:2011-08-15 15:39:56
【问题描述】:

我是编程新手,尝试将文件从一个位置复制到另一个位置,尝试在 Visual Studio 2010 中使用 app.config 文件。

配置文件是

<?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>

复制文件的代码是

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 for 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();

            }
        }
    }
}

我在构建解决方案时遇到错误,提示“System.Configuration.ConfigurationSettings”不包含“appSettings”的定义

我做错了什么? 谢谢您的帮助。

【问题讨论】:

    标签: .net c#-4.0


    【解决方案1】:

    ConfigurationManager 已过时,需要在.NET Core 环境中使用IConfiguration(IConfiguration 由.NET Core 内置依赖注入提供)。

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

    【讨论】:

      【解决方案2】:

      您需要使用配置管理器:

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

      【讨论】:

      • 我是否需要更改其他任何内容。将上述代码复制到我的代码中时出现错误命名空间“System.Configuration”中不存在类型或命名空间名称“ConfigurationManager”(您是否缺少程序集引用?)
      • @DrShah:您可能必须包含一个 using 子句:using System.Configuration;
      • @IAbstract,如果您像我的示例那样在语句中使用完整的程序集路径,则不会。但是,@DrShah 您需要向您的项目添加对 System.Configuration 程序集的引用。它在 GAC 中。然后,您可以从那里添加using System.Configuration; 并将我的示例中的语句截断为简单的ConfigurationManager.AppSettings["sourcePath"]
      • @DrShah:另外,请确保将项目引用添加到 System.configuration 命名空间。
      • 是的,我遇到了一个错误,缺少参考并添加了它。非常感谢。我是新手,仍然掌握编程的窍门。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多