public void Copy(
string sourceFilePath,
string destinationFilePath,
string destinationFileName = null)
{
if (string.IsNullOrWhiteSpace(sourceFilePath))
throw new ArgumentException("sourceFilePath cannot be null or whitespace.", nameof(sourceFilePath));
if (string.IsNullOrWhiteSpace(destinationFilePath))
throw new ArgumentException("destinationFilePath cannot be null or whitespace.", nameof(destinationFilePath));
var targetDirectoryInfo = new DirectoryInfo(destinationFilePath);
// this creates all the subdirectories, too
if (!targetDirectoryInfo.Exists)
targetDirectoryInfo.Create();
var fileName = string.IsNullOrWhiteSpace(destinationFileName)
? Path.GetFileName(sourceFilePath)
: destinationFileName;
File.Copy(sourceFilePath, Path.Combine(destinationFilePath, fileName));
}
//I declare first my variables
string sourcePath = @"Z:\SourceLocation";
string targetPath = @"Z:\TargetLocation";
string destFile = Path.Combine(targetPath, fileName);
string sourceFile = Path.Combine(sourcePath, 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);
8条答案
按热度按时间myzjeezk1#
这对我很有效:
r1zhe5dt2#
如果目标目录不存在,
File.Copy
将抛出。这个版本解决了在.NET Core 2.1上测试
pgx2nnw83#
我用这个代码和它为我工作
lpwwtiir4#
NET 6-扩展方法
b0zn9rqh5#
xesrikrc6#
工作正常.
s4n0splo7#
MSDN File.Copy
mrwjdhj38#
也许
?