Click here to Skip to main content
15,936,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I am trying to read from multiple directories of xml files and select certain ones to be placed in one folder. Everything goes well until I get an error message at the "File.WriteAllText" function. I verified the directory being written to does exist. 4cenUs.xml is also the first file that is suppose to be written to the directory.

Below is the code
It says:
can not find part of the path at C:\pg\XMLPull\xmlFiles2015\4cenUS.xml

DirectoryInfo di = Directory.CreateDirectory(@"C:\pg\XMLPull\xmlFiles2015\");

var getFiles = Directory.GetFiles(@"\\Company\file\","*xml",SearchOption.AllDirectories).ToList();

            DirectoryInfo di = Directory.CreateDirectory(;C:\pg\XMLPull\xmlFiles2015\);
           getFiles.ForEach(file =>
               var fileContents = File.ReadAllText(file);

               if (!fileContents.Contains(Diagnostics<systemname>))


                       File.WriteAllText(C:\pg\XMLPull\xmlFiles52015\ + Path.GetFileName(file), fileContents)
[no name] 13-Oct-15 10:15am    
You are getting error like " C:\pg\XMLPull\xmlFiles2015\4cenUS.xml" but you used path in your code as "C:\pg\XMLPull\xmlFiles52015\". If you see closely you are missing "5" in "xmlFiles52015".

Please cross check your path is completely correct.

1 solution

   File.WriteAllText(@"C:\pg\XMLPull\xmlFiles52015\" + Path.GetFileName(file), fileContents);

There is a typo. You could put the root directory in a string const to avoid this kind of problem.
Share this answer
Member 12009796 13-Oct-15 11:02am    
Thank you I can't believe I miss that everything is working fine now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900