Systerm.- Konfiguration.ConfigurationSettings enthält keine definition für appSettings
Ich bin neu in die Programmierung, versuchen das kopieren von Dateien von einem Ort zum anderen, versucht die app.config-Datei in Visual Studio 2010.
Die config-Datei
<?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>
Den code um die Datei zu kopieren ist
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();
}
}
}
}
Bekomme ich eine Fehlermeldung, wenn ich die Projektmappe erstellen, die sagt " System.- Konfiguration.ConfigurationSettings " enthält keine definition für 'appSettings'
Was mache ich falsch?
Vielen Dank für die Hilfe.
Du musst angemeldet sein, um einen Kommentar abzugeben.
Müssen Sie die configuration manager:
using System.Configuration;
System.Configuration
Montage um Ihr Projekt. Es ist in den GAC. Von dort aus können Sie dann fügen Sie dieusing System.Configuration;
und kürzen Sie die Anweisung in meinem Beispiel einfachConfigurationManager.AppSettings["sourcePath"]
System.configuration
namespace.