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 に答える