开发者

Getting an exception as "The parameter is incorrect.\r\n" while moving file

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


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


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜