Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to C#. I am working on a Windows Form that should do the following;
1- Browse files on local drive
2- allow the user to select files
3- list the selected files in a listBox
4- allow the user to input new file name and when click the rename button it rename the selected file in the listBox.

I am not able to do step 4 as the new text is changed in the listBox but the actual file name is still the same in folder. Please how can i do that... I have listed below the form.cs Thank you

What I have tried:

C#
public partial class everSupportForm : Form
{
    public everSupportForm()
    {
        InitializeComponent();
    }

    private void buttonSelect_Click(object sender, EventArgs e)
    {


        System.IO.Stream myStream;

        var myDialog = new OpenFileDialog();

        myDialog.InitialDirectory = @"c:\";
        myDialog.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
        //  + "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" //If you want to add filters for browsing only images.
        myDialog.FilterIndex = 1;
        myDialog.RestoreDirectory = true;
        myDialog.Multiselect = true;
        myDialog.Title = "Please Select File(s) to Rename";

        if (myDialog.ShowDialog() == DialogResult.OK)
        {
            foreach (var file in myDialog.FileNames)
            {
               try
                {
                    if ((myStream = myDialog.OpenFile()) != null)
                    {
                        using (myStream)
                        {

                       outputListBox.Items.Add(System.IO.Path.GetFileName(file));

                        }
                    }
                }

                catch (Exception ex)
                {
                 Could not load File specifying the causes 
               MessageBox.Show("Cannot display the File");
                }
            }
        }

    }

    private void buttonExit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    //buttonRemove method to remove a selected item
    private void buttonRemove_Click(object sender, EventArgs e)
    {
        if (outputListBox.SelectedIndex >=0)
        {
            outputListBox.Items.RemoveAt(outputListBox.SelectedIndex);

        } //End of button remove method
    }

    // buttonClear method to clear the listed images ListBox
    private void buttonClear_Click(object sender, EventArgs e)
    {
        outputListBox.Items.Clear();
    } // End of Clear Method

    private void everSupportForm_Load(object sender, EventArgs e)
    {

    }

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

            outputListBox.Items[outputListBox.SelectedIndex] = newNametextBox.Text;
        }
        else
        {
            MessageBox.Show("There is no Files in the Above list to be Selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
Posted
Updated 23-Aug-18 7:50am
v2

1 solution

You're only updating the name displayed in the ListBox.  You need to call File.Move()[^] in order to rename the file.

I recommend renaming the file(s) and then simply refreshing the ListBox's contents, since you can't assume the rename will always work.  Also, be sure to catch exceptions and display an appropriate error to the user.
 
Share this answer
 
v2
Comments
Member 13956894 23-Aug-18 16:10pm    
Thank you for your reply. The idea is to select a file from the listBox and rename it.
You were right regarding the GetFileName. I updated that line of code to be outputListBox.Items.Add(file);

but, I still have not managed to get the file renamed by using the File.Move() method.
Thank you
Ravi Bhavnani 23-Aug-18 22:51pm    
Are you specifying correct parameters to File.Move()? IOW, are the old and new filenames fully qualified and valid?

/ravi
Member 13956894 24-Aug-18 14:39pm    
Thank you so much for your Remark
Ravi Bhavnani 24-Aug-18 16:36pm    
All good now? Were you able to get the rename working?

/ravi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900