收到异常“参数不正确。\r\n”移动文件时

发布于 2024-09-24 07:45:35 字数 725 浏览 0 评论 0原文

我已经编写了一段代码来移动文件,如下所示

            private void Move_Click(object sender, EventArgs e)
    {
        string strOrgpath = string.Empty, strNewpath = string.Empty;
        strOrgpath = tvwACH.SelectedNode.ToString();
        string strPath = strOrgpath.Substring(10);
        FolderBrowserDialog folderborwser1 = new FolderBrowserDialog();

       if (folderborwser1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                strNewpath = folderborwser1.SelectedPath;
                File.Move(strPath, strNewpath);
            }
            catch (Exception ex)
            {

            }
        }

    }

,但是正如我提到的,我遇到了异常,任何人都可以告诉我原因,有时我会收到错误,因为对路径的访问被拒绝

I have written a code to move a file as follows

            private void Move_Click(object sender, EventArgs e)
    {
        string strOrgpath = string.Empty, strNewpath = string.Empty;
        strOrgpath = tvwACH.SelectedNode.ToString();
        string strPath = strOrgpath.Substring(10);
        FolderBrowserDialog folderborwser1 = new FolderBrowserDialog();

       if (folderborwser1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                strNewpath = folderborwser1.SelectedPath;
                File.Move(strPath, strNewpath);
            }
            catch (Exception ex)
            {

            }
        }

    }

But i am getting the exception as i mentioned can any one tell why and some times i am getting the error as access to the path is denied

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走走停停 2024-10-01 07:45:35

确保您的子字符串调用返回正确的结果。如果可能,请改用 Path 类中的静态方法。查看 MSDN 页面的 File.Move< /a> 并注意需要哪些参数 - 您应该提供两个有效的完整文件名(例如 C:\Blah\myFile.txt)。

如果用户在文件夹浏览器对话框中选择他们没有写入权限的文件夹,则可能会出现“访问被拒绝”错误消息。您必须在代码中处理这种情况,也许可以通过捕获 UnauthorizedAccessException 来处理。

更新:目标文件还应该指向文件名。所以你需要做这样的事情:

var origFileName = Path.GetFileName(strPath);
strNewpath = Path.Combine(folderborwser1.SelectedPath, origFileName);
File.Move(strPath, strNewpath);

Make sure your substring call returns the correct result. If possible, use static methods from the Path class instead. Take a look at the MSDN page for File.Move and pay attention to what parameters are expected -- you should provide two valid full file names (e.g. C:\Blah\myFile.txt).

"Access denied" error message might happen if the user picks a folder they don't have write access to in the folder browser dialog. That's a scenario you'll have to handle in your code, perhaps by catching the UnauthorizedAccessException.

Update: the destination file should also point to a filename. So you'll need to do something like this:

var origFileName = Path.GetFileName(strPath);
strNewpath = Path.Combine(folderborwser1.SelectedPath, origFileName);
File.Move(strPath, strNewpath);
别低头,皇冠会掉 2024-10-01 07:45:35

如果没有看到运行时应用程序中使用的值,我猜测 tvwACH.SelecteNode.ToString()strOrgpath.Substring(10) 不是有效的文件系统路径。

您应该调试您的应用程序并设置一个断点以查看这些值是什么(如果此时您的问题不明显,则发布它们)。

Without seeing the values that are being used in your application at run-time, I'm guessing tvwACH.SelecteNode.ToString() or strOrgpath.Substring(10) is not a valid File System path.

You should Debug your application and set a breakpoint to see what those values are (and post them if it's not obvious what your problem is at that point).

情话已封尘 2024-10-01 07:45:35

出现同样的错误,因为我试图在文件名中使用此符号之一
目标路径 '\ / : * ? “<>|'。

Had the same error because i was trying to use one of this symbols in file name
of destination path '\ / : * ? " < > |'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文