0

I am currently working with Copy and Paste with Item (file name) in Listbox. There's no error but copy and paste seems to not be working. I am new to this so I don't know what is the problem here, any help would be appreciated.

Code in Copy

 if(lvwExplorer.SelectedItems[0].Text != "" && lvwExplorer.SelectedItems.Count == 1)
        {
            Clipboard.SetText(lvwExplorer.SelectedItems[0].Text);
        }
        else
        {
            MessageBox.Show("You can only copy one element at a time.", "Cannot Copy", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

Code in Paste

string path = Clipboard.GetText();
        char seperator = '\\';
        string originalFileName = path.Split(seperator)[path.Split(seperator).Length - 1];
        string target = cbxAddress.Text + "\\" + originalFileName;

        try
        {
            if(File.Exists(target))
            {
                if (MessageBox.Show("The File you want to copy already exists. Do you want to replace it?", "File exists", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    File.Delete(target);
                    File.Copy(path, target, false);
                    GoToDirectory();
                }
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show("Error " + ex.Message);
        }
    }
Kyle Ross
  • 87
  • 9
  • 1
    what you trying to do? Is this copy and paste all within your own program? Does the paste code run? – pm100 Dec 11 '18 at 19:17
  • Yes copy and paste within the program. Paste doesn't seem working – Gabriel Apostol Dec 11 '18 at 19:19
  • Note that the expression in the `if` statement for your copy operation is processed from left to right. You are trying to access a selected item _before_ checking if a selected item exist. Do it the other way round ;-) Also, use the debugger to see whether Clipboard.SetText(...) is actually being called in reality. In the same manner, use the debugger to verify that your paste code is actually being executed. And while you are using the debugger there, also inspect and verify the values of all the variables end elements your code is working with. –  Dec 11 '18 at 19:19
  • You should probably use `string originalFileName = Path.GetFileName(path);` and `string target = Path.Combine(cbxAddress.Text, originalFileName);` – Rufus L Dec 11 '18 at 19:20
  • The file-already-exists message is missing a "to", as in "The File you want to copy **to** already exists...". – Andrew Morton Dec 11 '18 at 19:22
  • 4
    Did you bother to use the debugger to find out *why* it doesnt work? **[Navigating through Code using the Step Debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)** – Ňɏssa Pøngjǣrdenlarp Dec 11 '18 at 19:22
  • does that paste code actually get run? put a breakpoint there and walk through it – pm100 Dec 11 '18 at 19:24
  • 2
    please use System.IO.Path to manipulate paths, rather than all that ugly split code – pm100 Dec 11 '18 at 19:25

1 Answers1

3

In the Paste code, the Paste operation is done only when the target file exists! Please change your code:

            ... 
            if(File.Exists(target))
            {
                if (MessageBox.Show("The File you want to copy already exists. Do you want to replace it?", "File exists", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    File.Delete(target);
                    File.Copy(path, target, false);
                    GoToDirectory();
                }
            }
            else
            {
                File.Copy(path, target, false);
                GoToDirectory();
            }
            ...
M.Mahdipour
  • 592
  • 1
  • 7
  • 21