3
string str = "C:\\efe.txt";
string dir = "D:\\";

我想移动或复制“D:\”目录下的“efe.txt”文件。我怎样才能做到这一点。

谢谢你的建议.....

4

3 回答 3

6

正如其他人提到的那样,您想使用File.Move,但鉴于您的输入,您也想使用Path.CombinePath.GetFileName 喜欢这样

string str = "C:\\efe.txt";
string dir = "D:\\";
File.Move(str, Path.Combine(dir, Path.GetFileName(str)));
于 2013-02-18T18:15:12.397 回答
5

来自 MSDN:如何:复制、删除和移动文件和文件夹(C# 编程指南)

// Simple synchronous file move operations with no user interface. 
public class SimpleFileMove
{
    static void Main()
    {
        string sourceFile = @"C:\Users\Public\public\test.txt";
        string destinationFile = @"C:\Users\Public\private\test.txt";

        // To move a file or folder to a new location:
        System.IO.File.Move(sourceFile, destinationFile);

        // To move an entire directory. To programmatically modify or combine 
        // path strings, use the System.IO.Path class.
        System.IO.Directory.Move(@"C:\Users\Public\public\test\", @"C:\Users\Public\private");
    }
}
于 2013-02-18T18:09:44.227 回答
3

尝试File.Move

using System.IO;
...
string src = "C:\\efe.txt";
string dest = "D:\\efe.txt";
File.Move(src, dest);
于 2013-02-18T18:10:47.007 回答