views:

43

answers:

3

Hi all, 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

A: 

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

Justin Niessner
I am getting the values correctly. Initial path will be the treenode so i have taken the substring of that and assigned to another string
Dorababu
+1  A: 

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);
Anna Lear
My substring is returning correct result only i am getting the path as "c:\some.txt" and i would like to move this to some other destination selected by user from folderbrowserdialog
Dorababu
The destination for the move should also be a filename. I updated my answer to include some sample code.
Anna Lear
Thanks for giving an idea :)
Dorababu
A: 

Dear anna,

I have written my code as follows from seeing your code

       string strOrgpath = string.Empty, strNewpath = string.Empty;
        strOrgpath = tvwACH.SelectedNode.ToString();
        string strPath = strOrgpath.Substring(10);
        //string strPath1 = Path.GetFileName(strPath);
        FolderBrowserDialog folderborwser1 = new FolderBrowserDialog();
                    if (folderborwser1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                   //Just edited this as follows it works 
                strNewpath = Path.Combine(folderborwser1.SelectedPath,Path.GetFileName(strPath));
                File.Move(strPath, strNewpath);
            }
            catch (Exception ex)
            {

            }
        }

But the Newpath is also getting the same as old path even if i select the destination as different folder

Dorababu
what's the value of "strPath"?
Anna Lear