i new programming, trying copy files 1 location another, trying use app.config file in visual studio 2010.
the config file
<?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 copy 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 manipulate file , directory paths. string sourcefile = system.io.path.combine(sourcepath, filename); string destfile = system.io.path.combine(targetpath, filename); // copy folder's contents new location: // create new target folder, if necessary. if (!system.io.directory.exists(targetpath)) { system.io.directory.createdirectory(targetpath); } // copy file location , // overwrite destination file if exists. system.io.file.copy(sourcefile, destfile, true); // copy files in 1 directory directory. // files in source folder. (to recursively iterate through // subfolders under current directory, see // "how to: iterate through directory tree.") // note: check target path performed // in code example. if (system.io.directory.exists(sourcepath)) { string[] files = system.io.directory.getfiles(sourcepath); // copy files , overwrite destination files if exist. foreach (string s in files) { // use static path methods extract file name 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 not exist!"); } // keep console window open in debug mode. console.writeline("press key exit."); console.readkey(); } } } }
i error when build solution says 'system.configuration.configurationsettings' not contain definition 'appsettings'
what doing wrong? help.
you need use configuration manager:
system.configuration.configurationmanager.appsettings["sourcepath"]
Comments
Post a Comment