Copy file to folder

string cpath = listBox2.SelectedIndex.ToString();

string cpath2 = cpath + "temp";

if(cpath.Substring(cpath.Length-3,3).ToLower() == "xml")

{

File.Copy(cpath, cpath2);

}

I have a listbox of folders and when i click a folder the files appear in my listbox2...i want to select one the files click by button and that file is copied to my c:\\temp folder.....I am getting the exception that my start index must be less than zero....any help...thanks!




Answer this question

Copy file to folder

  • AtomZ .be

    I have two listboxes one list files and another just list the c:\\temp directory...what i want to do is copy the file i select in the first listbox to the c:\\temp direcotry which is in the second listbox by clicking my button1....where am i going wrong.....

    private void button1_Click(object sender, System.EventArgs e)

    {

    string cpath = listBox2.SelectedValue.ToString();

    string cpath2 = listBox3.SelectedItem.ToString();

    if(cpath.EndsWith("xml"))

    {

    File.Copy(cpath, cpath2); it breaks here......"The target direcotry already exsist"

    }

    else

    {

    MessageBox.Show("File could not be copied");

    }

    }



  • Diango

    I feel you might be getting an error that the start index can not be less than zero Check the value of SelectedIndex. It might be -1 because nothing in the listbox is selected.

  • Dattaforit

    Tryin2BeGood what you probably want is

    if (String.Equals(System.IO.Path.GetExtension(cpath), "xml", StringComparison.InvariantCultureIgnoreCase)==true)

    System.IO.Path.GetExtension gets the file extension of the file name.
    The String.Equals in combination with InvariantCultureIgnoreCase allows you to match
    xml, XmL XML etc

    Note that this will match
    test1.xml
    but not a file called myxml

    If you don't want to go by file extension then I suggest using
    if (cpath.EndsWith("xml", S
    tringComparison.InvariantCultureIgnoreCase)==true

    System.IO.Path has alot of useful functions for doing things to file names and paths.

    Hope this helps.

  • norpe

    File.Copy() expects filenames as its two parameters, but you're passing a folder name as the second parameter.

    Try changing your File.Copy() so that its second parameter specifies the full filename of the destination file:

    File.Copy(cpath, cpath2+"\\"+Path.GetFileName(cpath));


  • Can-Ann



    if (Path.GetExtension(cpath).Equals(".xml", StringComparison.InvariantCultureIgnoreCase)==true)
    {
    File.Copy(cpath, Path.Combine(@"C:\temp", Path.GetFileName(cpath)));
    }

    As my prior post stated you use functions like
    GetExtension and Equals in combination with StringComparison.InvariantCultureIgnoreCase to ensure that your program works the way people expect it to and something as simple as changing the case of the file extension does not work.

    File.Move MOVES, it does not copy. Throughout your post you explicitly state that you are trying to copy.
    You will also notice I used 2 other functions: Path.GetName and Path.Combine

    Whenever you are putting paths together you should use Path.Combine instead of using " \" + ... etc. Path.Combine ensures that you do not have to worry about whether one part contained a \ or a / or whether it didn't. It will put them there for you. Path.GetFileName retrieves the filename part of a full path.




  • eevee

    I don't know at what line you're getting the exception, but you'd get that exception with the code "cpath.SubString(cpath.Length-3)" because you're not checking that cpath.Length >= 3.

  • sally_de

    Tryin2Bgood wrote:

    string cpath = listBox2.SelectedIndex.ToString();


    that should really be
    if (listBox2.SelectedIndex==-1)
    { return; }
    String cpath = listBox2.SelectedValue;

    You SHOULD use my code from above however also in addition to correcting this error.

  • Davids Learning

    I also tried it like so, but the compiler tells me that it Cannot create a file when that file already exists....I know the file already exsist i just want to move it too c:\\temp....would it have anything to do with how my files show up in the listbox...they list in the listbox  like so c:\test\data\myfile.xml do i need to trim off everything but myfile.xml  

     

    private void button1_Click(object sender, System.EventArgs e)

    {

    string cpath = listBox2.SelectedItem.ToString();

    //string cpath2 = listBox3.SelectedItem.ToString();

    if(cpath.EndsWith("xml"))

    {

    MessageBox.Show("hello");

    File.Move(cpath, "c:\\temp");

    }

    else

    {

    MessageBox.Show("File could not be copied");

    }



  • Copy file to folder