简体   繁体   中英

How to copy a file from one location to another in C#

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"

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

I had two problems. I didn't have a targetPath written in the code and was therefore trying to copy to the sourcePath. I was able to get it working and then I got a permission denied message for the targetPath. I realized I didn't have the file in the directory for the program to copy to the sourcePath. I fixed that and the program runs perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM