使用C#将文件移动到目录下

时间:2013-02-18 18:08:24

标签: c# file copy directory move

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

我想在“D:\”目录下移动或复制“efe.txt”文件。我怎么能这样做。

感谢您的建议.....

3 个答案:

答案 0 :(得分:5)

来自MSDN: How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)

// 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");
    }
}

答案 1 :(得分:5)

正如其他人提到的那样,您想要使用File.Move,但根据您的输入,您还需要使用Path.CombinePath.GetFileName这样

string str = "C:\\efe.txt";
string dir = "D:\\";
File.Move(str, Path.Combine(dir, Path.GetFileName(str)));

答案 2 :(得分:3)

尝试File.Move

using System.IO;
...
string src = "C:\\efe.txt";
string dest = "D:\\efe.txt";
File.Move(src, dest);