none
How to rename multiple images in a List-Box in C# windows application RRS feed

  • Question

  • I am new to C#. I am building a windows form that can rename multiple files in a List Box. 

    Files are actually images (.png) and what i am trying to do is just rename the images from "image1.png, image2.png, image3.png", ... etc , to "newname1.pnage, newname2.png, newname3.png"

    I was successful to make it work for renaming single image, however and I think there must me a for loop there to do the multiple image rename (I might be wrong). So would you kindly helping me resolving this issue?.

    here is my code of the rename button.... Thank you.

    private void buttonRename_Click(object sender, EventArgs e)
            {
                if (outputListBox.SelectedIndex >= 0)
                {

                    string fileToRename = outputListBox.Items[outputListBox.SelectedIndex].ToString();
                     string newFileName = newNametextBox.Text;

                    if (!string.IsNullOrEmpty(newFileName))
                    {
                        string fileName = Path.GetFileName(fileToRename);
                        string newFilePath = fileToRename.Replace(fileName, newFileName);
                        System.IO.File.Move(fileToRename, newFilePath);
                        outputListBox.Items[outputListBox.SelectedIndex] = newFilePath;
                    }
                }
                else
                {
                    MessageBox.Show("There is no Files in the Above list to be Selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    • Edited by Hanseller Monday, September 17, 2018 6:08 PM
    Monday, September 17, 2018 4:18 PM

All replies

  • You can do something like this:

    var newFileName = "newName";
    var fileToRename = "";
    var idx = 1;
    
    foreach(var selectedItem in outputListBox.SelectedItems)
    {
        fileToRename = selectedItem.ToString();
    
        if (!string.IsNullOrEmpty(newFileName))
        {
            newFileName += idx.ToString(); 
            string fileName = Path.GetFileName(fileToRename);
            string newFilePath = fileToRename.Replace(fileName, newFileName);
            System.IO.File.Move(fileToRename, newFilePath);
        }
    }

    However, you can not change the selected item in this foreach loop. You need to reload the list or collect new file names and indexes and create another loop that will change the elements of the listbox.

    Monday, September 17, 2018 5:50 PM
  • Thank you for your reply, however, I can go and rename them one by one as my code shows it. I know there must a for loop to go through the selected images and rename them with the new given name. I do not know how. ... Thanks again


    Hanseller

    Tuesday, September 18, 2018 6:24 PM
  • Hi Hanseller,

    If you do not change the PictureBox name and use the default name, you could try the following code to foreach the PictureBox name one by one and change it.

               for (int i = 0; i < list.Count; i++)
                {
                    PictureBox pb = Controls["pictureBox" + (i + 1).ToString()] as PictureBox;
                    if (pb != null)
                    {
                        pb.Name = xxxxxx;//change the name
                    }
                }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 28, 2018 1:39 AM
    Moderator