2013-02-18 81 views
3
string str = "C:\\efe.txt"; 
string dir = "D:\\"; 

我想移動或複製「d:\」下的「efe.txt」文件的目錄下的目錄。我怎樣才能做到這一點。文件移動到使用C#

謝謝你的建議.....

回答

5

正如其他人所說,你要使用File.Move,但考慮到您的意見,您還需要使用Path.CombinePath.GetFileName like so

string str = "C:\\efe.txt"; 
string dir = "D:\\"; 
File.Move(str, Path.Combine(dir, Path.GetFileName(str))); 
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"); 
    } 
} 
3

嘗試File.Move

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