【问题标题】:Same path works fine with files and does not with folder相同的路径适用于文件而不适用于文件夹
【发布时间】:2014-01-02 04:07:02
【问题描述】:
 private void bt_edit_folder_name_Click(object sender, EventArgs e)
    {
        Directory.Move("\\\\192.168.1.244\old_name", "\\\\192.168.1.244\new_name");
        MessageBox.Show("Done");
    }

我使用相同的路径来更改文件名,它工作正常,但我不能使用相同的路径来更改文件夹名称。 它显示此调试错误:

The specified path is invalid

【问题讨论】:

  • 这样写,您的字符串将包含换行符和不存在的转义字符\o - 您的实际代码是什么?

标签: c# path directory


【解决方案1】:

您可以使用verbatim string 来解决您的问题

 Directory.Move(@"\\192.168.1.244\old_name", @"\\192.168.1.244\new_name");

【讨论】:

  • 非常感谢它现在可以很好地处理文件和文件夹(目录),我花了很多时间试图解决这个问题。 :)
【解决方案2】:

如果您在使用 c# 移动目录时遇到问题,那么您可以尝试使用应该可以工作的进程来处理 cmd 命令,

    String command="xcopy "+srcPath+" "+destPath+" /i /q /s /y";
    Process p = Process.Start(new ProcessStartInfo()  {
        FileName = "cmd",
        Arguments = "/c \"" + command + "\"",
        RedirectStandardError = true,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        UseShellExecute = false
    });
    p.Start();
    string output = p.StandardOutput.ReadToEnd();//read error & feedback messages
    string error = p.StandardError.ReadToEnd();
    p.WaitForExit();

【讨论】:

    【解决方案3】:

    获得此异常的唯一方法是不符合命名约定。 The documentation states:

    sourceDirName 或 destDirName 是长度为零的字符串,仅包含空格,或者包含一个或多个由 InvalidPathChars 定义的无效字符。

    所以,换句话说,destDirName 是无效的。

    【讨论】:

      【解决方案4】:

      试试这个

      var dir = new DirectoryInfo(@"\\192.168.1.244\old_name");
      dir.MoveTo(@"\\192.168.1.244\tmpName");
      dir.MoveTo(@"\\192.168.1.244\new_name");
      

      【讨论】:

        猜你喜欢
        • 2013-01-11
        • 1970-01-01
        • 2014-10-16
        • 1970-01-01
        • 2018-09-03
        • 1970-01-01
        • 2018-12-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多