作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图将一个文件从桌面移动到一个名为“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/
我是一名优秀的程序员,十分优秀!