首页 > 解决方案 > 重命名 C# windows 窗体 listBox 上列出的文件

问题描述

我是 C# 新手。我正在开发一个应该执行以下操作的 Windows 窗体:

  1. 浏览本地驱动器上的文件
  2. 允许用户选择文件
  3. 在列表框中列出选定的文件
  4. 允许用户输入一个新的文件名,当他们单击重命名按钮时,它会重命名ListBox.

我无法执行第 4 步,因为列表框中的新文本已更改,但文件夹中的实际文件名仍然相同。我怎样才能做到这一点?我在 Form.cs 下面列出了

谢谢你。

public partial class everSupportForm : Form
{
    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) return;
        {
            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();

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

    // Clears the listed images ListBox
    private void buttonClear_Click(object sender, EventArgs e) => outputListBox.Items.Clear();
    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);
    }
}

标签: c#

解决方案


首先,你这里有问题。
您正在使用添加文件名Path.GetFileName,因此您将不再拥有文件路径=>您无法重命名它们。

解决方案 1 - 将路径添加到列表框

Path.GetFileName当单击重命名按钮时,您只需删除and ,然后使用 anInputBox向用户询问新文件名:注意:添加对,
的引用位于命名空间中。Microsoft.VisualBasicInputBoxMicrosoft.VisualBasic.Interaction

private void buttonRename_Click(object sender, EventArgs e)
{
    if (outputListBox.SelectedIndex >=0)
    {
        string fileToRename = outputListBox.Items[outputListBox.SelectedIndex].ToString();
        string newFileName = InputBox("Please enter the new file's name:", "Rename file", "Default value");
        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);
    }
}

解决方案 2 -- 保留一个包含路径的单独列表

该概念与第一个解决方案相同,但不是将文件路径添加到列表框,而是添加文件名,但保留与列表框同步的单独路径列表。
这意味着,如果您修改列表框,请修改路径列表。

之后,您可以使用列表框访问文件路径SelectedIndex(因为它们已同步)。


推荐阅读