gpt4 book ai didi

c# - 如何将文件移动到目录而不替换文件?

转载 作者:行者123 更新时间:2023-11-30 20:32:31 24 4
gpt4 key购买 nike

我试图将一个文件从桌面移动到一个名为“Textfiles”的目录,但每次尝试都会出现此错误。

Additional information: The target file "C:\Users\Developer\Documents\Textfiles" is a directory, not a file.

现在我知道使用

File.Copy(fileName, targetPath);

这是错误的,这就是我现在正在使用的,它有两个参数,第一个是 yopu 想要复制的文件,第二个是它要替换的文件?如果我在第二个参数上有误,请纠正我。

无论如何,我也尝试了 System.IO.Directory.Move(fileName, destFile);,但这几乎给了我同样的错误。

这两个参数很简单,就是两个由路径组成的字符串。

string fileName = filePath.ToString();
string targetPath = @"C:\Users\Developer\Documents\Textfiles";

将 fileName 传输到 targetPath 的正确方法是什么?

最佳答案

您需要指定目标文件名。

string fileOnly = System.IO.Path.GetFileName(fileName);
string targetPath = System.IO.Path.Combine(@"C:\Users\Developer\Documents\Textfiles", fileOnly);
System.IO.File.Move(fileName, targetPath);

关于c# - 如何将文件移动到目录而不替换文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41321969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com