Copy file from one directory to another

后端 未结 5 423
执笔经年
执笔经年 2021-01-28 13:11

I am pretty new to C# and I am trying to get my program to copy a file from one location to another. The method I have is as below;

    private void CopyInstallF         


        
5条回答
  •  悲哀的现实
    2021-01-28 13:46

    File.Copy requires the full filename for the destination.

    destFileName
    Type: System.String
    The name of the destination file. This cannot be a directory.

    If your input is just the folder name then you need to add the filename of the source file.

    private void CopyInstallFiles(object sender, EventArgs e)
    {
        // The correct syntax for a path name requires the verbatim @ char
        string sourceFile = @"F:\inetpub\ftproot\test.txt";
        string file = Path.GetFileName(sourceFile);
        string copyPathone = directoryImput.Text;
        System.IO.File.Copy(sourceFile, Path.Combine(copyPathone, file), true);
    }
    

    Note the final parameter = true to overwrite a file in the destination folder.

    As a side note, I suggest you to remove the textbox as input for a folder name but instead use the FolderBrowserDialog

提交回复
热议问题