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

Mah*_*EFE 3 c# directory copy file move

string str = "C:\\efe.txt";
string dir = "D:\\";
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的建议.....

Jar*_*ley 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");
    }
}
Run Code Online (Sandbox Code Playgroud)


juh*_*arr 5

正如其他人提到你想要使用的那样File.Move,但是根据你的输入,你也会想要使用Path.CombinePath.GetFileName 喜欢这样

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