locked
Using loop ! RRS feed

  • Question

  • i want using loop tp this code

    if
     (checkedListBox1.CheckedItems.Count > 0)
    {
    string newFileName = null ;
    foreach (string file in openFileDialog1.FileNames)
    {
    try
    {
    newFileName = System.IO.Path.ChangeExtension(file, comboBox1.Text);
    System.IO.File.Copy(file, newFileName);
    }
    catch
    {
    MessageBox.Show("The File which name is ( " + newFileName + " ) already exists" , "Warning" , MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }
    }
    because of 2 files exist appear message 2 times with files name
    thnx
    Thursday, August 26, 2010 8:28 PM

Answers

  • Thanks O.Lock for pointing out the copy-paste error.

    LeGendXP, try this code -

       if (checkedListBox1.CheckedItems.Count > 0)
       {
        string existingFiles = String.Empty;
    
        string newFileName = null;
        foreach (string file in openFileDialog1.FileNames)
        {
         try
         {
          newFileName = System.IO.Path.ChangeExtension(file, comboBox1.Text);
          System.IO.File.Copy(file, newFileName);
         }
         catch
         {
          existingFiles += newFileName + ", ";
         }
        }
    
        if (!String.IsNullOrWhiteSpace(existingFiles))
        {
         MessageBox.Show("The file(s) ( " + existingFiles.TrimEnd(' ', ',') + " ) already exist.",
          "Warning",
          MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
       }
    

    Mohit Chakraborty
    Monday, August 30, 2010 8:25 PM

All replies

  • You can store the file names in the catch block in a list and then show them all together in a single message box after the loop is done.
    Mohit Chakraborty
    Thursday, August 26, 2010 8:48 PM
  • can u write a code for me ?to understad more

    thnx

    Thursday, August 26, 2010 8:59 PM
  • What exactly do you want to achieve? Is it like Mohit suggests, you want all the file names to appear in 1 MessageBox?
    Thursday, August 26, 2010 10:01 PM
  • ye man i want on one messagebox or more thi important thing all files name appear thnx
    Thursday, August 26, 2010 10:18 PM
  • You can do something like -
          if (checkedListBox1.CheckedItems.Count > 0)
          {
            string existingFiles = String.Empty;
    
            string newFileName = null;
            foreach (string file in openFileDialog1.FileNames)
            {
              try
              {
                newFileName = System.IO.Path.ChangeExtension(file, comboBox1.Text);
                System.IO.File.Copy(file, newFileName);
              }
              catch
              {
                existingFiles += newFileName + ", ";
              }
            }
    
            if (!String.IsNullOrWhiteSpace(existingFiles))
            {
              MessageBox.Show("The file(s) ( " + newFileName.TrimEnd(' ', ',') + " ) already exist.",
                "Warning",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
          }

    Mohit Chakraborty
    Thursday, August 26, 2010 11:58 PM
  • same problem ! one name of a file only appear!

    what is wrong ?

     

    Friday, August 27, 2010 12:49 AM
  • How many file names are supposed to appear?
    Friday, August 27, 2010 7:48 PM
  • You can set a break-point inside the catch and see how many times the statement is hit and observe how 'existingFiles' changes.


    Mohit Chakraborty
    Monday, August 30, 2010 6:15 PM
  • In the Mohit's code in the MessageBox.Show function change 'newFileName' for 'existingFiles'
    Monday, August 30, 2010 6:46 PM
  • Thanks O.Lock for pointing out the copy-paste error.

    LeGendXP, try this code -

       if (checkedListBox1.CheckedItems.Count > 0)
       {
        string existingFiles = String.Empty;
    
        string newFileName = null;
        foreach (string file in openFileDialog1.FileNames)
        {
         try
         {
          newFileName = System.IO.Path.ChangeExtension(file, comboBox1.Text);
          System.IO.File.Copy(file, newFileName);
         }
         catch
         {
          existingFiles += newFileName + ", ";
         }
        }
    
        if (!String.IsNullOrWhiteSpace(existingFiles))
        {
         MessageBox.Show("The file(s) ( " + existingFiles.TrimEnd(' ', ',') + " ) already exist.",
          "Warning",
          MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
       }
    

    Mohit Chakraborty
    Monday, August 30, 2010 8:25 PM