【问题标题】:Move a file to under a directory using C#使用 C# 将文件移动到目录下
【发布时间】:2013-02-03 06:12:03
【问题描述】:
string str = "C:\\efe.txt";
string dir = "D:\\";

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

谢谢你的建议.....

【问题讨论】:

    标签: c# file copy directory move


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      来自 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);
        

        【讨论】:

          猜你喜欢
          • 2013-03-01
          • 2013-04-14
          • 2020-05-21
          • 2015-09-25
          • 1970-01-01
          • 1970-01-01
          • 2021-11-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多