【问题标题】:Moving a folder (Directory) from one location to another - misbehavior将文件夹(目录)从一个位置移动到另一个位置 - 行为不当
【发布时间】:2011-11-13 18:38:47
【问题描述】:

我想使用 C# .NET 将目录从一个位置移动到另一个位置。我以这种简单的方式使用了Directory.Move 甚至是 DirectoryInfo(带有 MoveTo):

// source is: "C:\Songs\Elvis my Man"
// newLocation is: "C:\Songs\Elvis"

try
{
    // Previous command was: Directory.Move(source, newLocation);
    DirectoryInfo dir = new DirectoryInfo(source);
    dir.MoveTo(newLocation);
}
catch (Exception e)
{
    Console.WriteLine("Error: "+ e.Message);
}

但是正在执行的操作(对于这两种情况)是将文件夹名称从“source”重命名为“newLocation”

我的预期是什么?文件夹“Elvis my man”现在将位于“Elvis”文件夹中。

发生了什么事?“Elvis my man”已更改为“Elvis”(重命名)。如果目录“Elvis”已经存在,则不能将其更改为“Elvis”(因为他不能重名),因此我得到一个例外。

我做错了什么??

非常感谢!!!

【问题讨论】:

    标签: c# directory move


    【解决方案1】:

    来自MSDN

    例如,如果您尝试将 c:\mydir 移动到 c:\public,并且 c:\public 已经存在,则此方法将引发 IOException。您必须将“c:\public\mydir”指定为 destDirName 参数,或者指定一个新的目录名称,例如“c:\newdir”。

    看来您需要将 newLocation 设置为 C:\Songs\Elvis\Elvis my man

    【讨论】:

      【解决方案2】:

      尽管这可以在命令行中移动文件,但在编程时您需要提供完整的新名称。

      因此,您需要将 newLocation 更改为“C:\Songs\Elvis\Elvis my Man”才能完成这项工作。

      【讨论】:

        【解决方案3】:

        我建议在 Move 命令周围进行验证,以确保源位置确实存在而目标位置不存在。

        我一直发现避免异常比一旦发生就处理它们更容易。

        您可能还希望包括异常处理,以防访问权限出现问题或文件已打开且无法移动...

        这里有一些示例代码:

                    string sourceDir = @"c:\test";
                string destinationDir = @"c:\test1";
        
                try
                {
                    // Ensure the source directory exists
                    if (Directory.Exists(sourceDir) == true )
                    {
                        // Ensure the destination directory doesn't already exist
                        if (Directory.Exists(destinationDir) == false)
                        {
                            // Perform the move
                            Directory.Move(sourceDir, destinationDir);
                        }
                        else
                        {
                            // Could provide the user the option to delete the existing directory
                            // before moving the source directory
                        }
                    }
                    else
                    {
                        // Do something about the source directory not existing
                    }
                }
                catch (Exception)
                {
                    // TODO: Handle the exception that has been thrown
                }
        

        【讨论】:

          【解决方案4】:
              private void moveDirectory(string fuente,string destino)
              {
                  if (!System.IO.Directory.Exists(destino))
                  {
                      System.IO.Directory.CreateDirectory(destino);
                  }
                  String[] files = Directory.GetFiles(fuente);
                  String[] directories = Directory.GetDirectories(fuente);
                  foreach (string s in files)
                  {
                      System.IO.File.Copy(s, Path.Combine(destino,Path.GetFileName(s)), true);     
                  }
                  foreach(string d in directories)
                  {
          
                      moveDirectory(Path.Combine(fuente, Path.GetFileName(d)), Path.Combine(destino, Path.GetFileName(d)));
                  }
          
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多