title: How to copy a file from one location to another in C#
body: <p>I got pieces of this code from some of the questions here but I could find none of them to address this specific need. I want to have this program run to log onto a server, copy the xml file in the location set and then update all user profiles with that xml file on all machines in the company. While the code isn't written to go from one server to other machines, I am testing it from one folder location to another on the same machine. However, I get this error, "The target file C:\Users\Public\TestFolder\MyCopyTest is a director, not a file." When I do list the file name I with a different folder, I get the directory created "Test Folder" with another directory inside it called "MyCopyTest.txt"</p>
  
  <pre><code>using System;
  using System.Configuration;
  using System.IO;
  
  namespace copy_delete_move_files
  {
      public class SimpleFileCopy
      {
          public static object Logger { get; private set; }
  
          static void Main()
          {
              string fileName = "test.txt";
              string sourcePath = @"C:\Users\Public\Test Folder";
              string targetPath = @"C:\Users\Public\TestFolder\SubDir";
  
              // Use Path class to manipulate file and directory paths.
              string sourceFile = Path.Combine(sourcePath, fileName);       
              string destFile = Path.Combine(targetPath, fileName);
  
              // To copy a folder's contents to a new location:
              // Create a new target folder, if necessary.
              if (!Directory.Exists(targetPath))
              {
                  Directory.CreateDirectory(targetPath);
              }
  
              // To copy a file to another location and 
              // overwrite the destination file if it already exists.
              File.Copy(sourceFile, destFile, true);
  
              // To copy all the files in one directory to another directory.
              // Get the files in the source folder.
              // Note: Check for target path was performed previously
              // in this code example.
  
              if (Directory.Exists(sourcePath))
              {
                  string[] files = 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 = Path.GetFileName(s);
                      destFile = Path.Combine(targetPath, fileName);
                      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();
          }
      }
  }
  </code></pre>