Hi all,
I am creating a program to assist with an upcoming job I have at work. I will be required to rename nearly 200 files with a different filename (but with same extension).
I have successfully created a program that can rename a file which I select from a listbox, but after renaming, the listbox doesnt update. I have searched around for information on this but without success. How do I make the listbox update after I click to rename a file.
My Windows Forms program contains
a listbox (lbxDir),
a button to list the files in the listbox (btnLbxDir),
a textbox for the new filename (tbxLbxDirNewFilename),
a combo box for the file extension (cboLbxDirExtensions)
a button to rename the file (btnLbxDirRenameFile).
a label which displays the selected directory (lblLbxDirectorySelected)
Basically, when you click the file list button, it will open a folder dialog. After selecting the directory, the filenames will list in the listbox. I select the file to rename, type a new name in the textbox and specify the extension in the combo and press the rename file button to rename the file. This all works fine. The file renames in the directory selected, but the listbox still shows the old name.
How can i make the listbox update after renaming? I have tried lbxDir.Update(); and lbxDir.Refresh(); and neither worked.
Thanks
I am creating a program to assist with an upcoming job I have at work. I will be required to rename nearly 200 files with a different filename (but with same extension).
I have successfully created a program that can rename a file which I select from a listbox, but after renaming, the listbox doesnt update. I have searched around for information on this but without success. How do I make the listbox update after I click to rename a file.
My Windows Forms program contains
a listbox (lbxDir),
a button to list the files in the listbox (btnLbxDir),
a textbox for the new filename (tbxLbxDirNewFilename),
a combo box for the file extension (cboLbxDirExtensions)
a button to rename the file (btnLbxDirRenameFile).
a label which displays the selected directory (lblLbxDirectorySelected)
C#:
private void btnLbxDir_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderPicker = new FolderBrowserDialog();
if (folderPicker.ShowDialog() == DialogResult.OK)
{
lbxDir.Items.Clear();
string[] files = Directory.GetFiles(folderPicker.SelectedPath);
lblLbxDirectorySelected.Text = folderPicker.SelectedPath.ToString() + "\\"; //show selected path in label text
//add the items in the directory to the listbox without the full path showing.
foreach (string filename in files)
{
lbxDir.Items.Add(System.IO.Path.GetFileName(filename));
}
}
}
private void btnLbxDirRenameFile_Click(object sender, EventArgs e)
{
string newfilename = tbxLbxDirNewFilename.Text;
string oldfileselect = lblLbxDirectorySelected.Text + lbxDir.SelectedItem.ToString();
string ext = cboLbxDirExtensions.Text.ToString();
string directory = lblLbxDirectorySelected.Text;
string fullfilenew = directory + newfilename + ext;
File.Move(oldfileselect, fullfilenew);
}
Basically, when you click the file list button, it will open a folder dialog. After selecting the directory, the filenames will list in the listbox. I select the file to rename, type a new name in the textbox and specify the extension in the combo and press the rename file button to rename the file. This all works fine. The file renames in the directory selected, but the listbox still shows the old name.
How can i make the listbox update after renaming? I have tried lbxDir.Update(); and lbxDir.Refresh(); and neither worked.
Thanks