简体   繁体   English

使用相对路径复制文件

[英]Copy file using relative path

File.Copy(@"my program\\subfolder\\what i want to copy.txt", "C:\\Targetlocation");

如何使用相对路径将文本文件从一个文件夹复制到另一个文件夹。

To execute the File.Copy the source and destination will be a valid file path. 要执行File.Copy ,源和目标将是有效的文件路径。 in your case the destination is a folder not File. 在您的情况下,目标是文件夹而不是文件。 in this case you may get some exception like 在这种情况下,您可能会遇到一些异常情况,例如

Could not find a part of the path 'F:\\New folder' 找不到路径“ F:\\ New folder”的一部分

While executing the application, the current directory will be the bin folder. 执行应用程序时,当前目录将是bin文件夹。 you need to specify the relative path from there. 您需要从此处指定相对路径。 Let my program/subfolder be the folders in your solution, so the code for this will be like this: my program/subfolder文件夹作为您解决方案中的文件夹,因此代码如下所示:

string sourcePath = "../../my program/subfolder/what i want to copy.txt";
string destinationPath = @"C:\Targetlocation\copyFile.txt"
File.Copy(sourcePath, destinationPath );

Where ../ will help you to move one step back from the current directory. ../将帮助您从当前目录后退一步。 One more thing you have to care is the third optional parameter in the File.Copy method. 您还需要注意的另一件事是File.Copy方法中的第三个可选参数。 By passing true for this parameter will help you to overwrite the contents of the existing file.Also make sure that the folder C:\\Targetlocation is existing, as this will not create the folder for you. 通过为该参数传递true将帮助您覆盖现有文件的内容。此外,请确保文件夹C:\\Targetlocation存在,因为这不会为您创建文件夹。

File.Copy(@"subfolder\\what i want to copy.txt", "C:\\Targetlocation\\TargetFilePath.txt");

The sourceFileName and destFileName parameters can specify relative or absolute path information. sourceFileName和destFileName参数可以指定相对或绝对路径信息。 Relative path information is interpreted as relative to the current working directory. 相对路径信息被解释为相对于当前工作目录。 This method does not support wildcard characters in the parameters. 此方法在参数中不支持通配符。

File.Copy on MSDN MSDN上的File.Copy

Make sure your target directory exists. 确保您的目标目录存在。 You can use Directory.CreateDirectory 您可以使用Directory.CreateDirectory

Directory.CreateDirectory("C:\\Targetlocation");

With Directory.CreateDirectory() , you don't have to check if the directory exists. 使用Directory.CreateDirectory() ,您不必检查目录是否存在。 From documentation: 从文档:

Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. 除非在路径中指定的目录已经存在或路径的某些部分无效,否则将创建该目录中指定的所有目录。 The path parameter specifies a directory path, not a file path. path参数指定目录路径,而不是文件路径。 If the directory already exists, this method does nothing. 如果目录已经存在,则此方法不执行任何操作。

// Remove path from the file name.
    string fName = f.Substring(sourceDir.Length + 1);

    try
    {
        // Will not overwrite if the destination file already exists.
        File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
    }

You can provide the relative path from your current working directory which can be checked via Environment.CurrentDirectoy. 您可以提供当前工作目录中的相对路径,可以通过Environment.CurrentDirectoy进行检查。

For example if your current working directory is D:\\App, your source file location is D:\\App\\Res\\Source.txt and your target location is D:\\App\\Res\\Test\\target.txt then your code snippet will be - 例如,如果当前工作目录为D:\\ App,源文件位置为D:\\ App \\ Res \\ Source.txt,目标位置为D:\\ App \\ Res \\ Test \\ target.txt,则您的代码段将是-

File.Copy(Res\\Source.txt, Res\\Test\\target.txt); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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