c# - 重命名 C# windows 窗体 listBox 上列出的文件
问题描述
我是 C# 新手。我正在开发一个应该执行以下操作的 Windows 窗体:
- 浏览本地驱动器上的文件
- 允许用户选择文件
- 在列表框中列出选定的文件
- 允许用户输入一个新的文件名,当他们单击重命名按钮时,它会重命名
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);
}
}
解决方案
首先,你这里有问题。
您正在使用添加文件名Path.GetFileName
,因此您将不再拥有文件路径=>您无法重命名它们。
解决方案 1 - 将路径添加到列表框
Path.GetFileName
当单击重命名按钮时,您只需删除and ,然后使用 anInputBox
向用户询问新文件名:注意:添加对,
的引用位于命名空间中。Microsoft.VisualBasic
InputBox
Microsoft.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
(因为它们已同步)。
推荐阅读
- javascript - 有 audioContext.encodeAudioData 方法吗?
- python - 如何使用包含参数数组的 for 循环运行 shell 脚本,每个命令都应在 ubuntu 的新终端窗口中运行
- amazon-web-services - 如何在 groovy 中评估美元表达式并存储值
- powershell - 将电子邮件保存在我的文件系统上的 msg 文件中
- ios - 如何将 RGBA 纹理转换为金属中的 Y 和 CbCr 纹理
- php - 如何将变量分配给选定的 ID 值以用于重复选择(获取)?
- c++ - c ++:易失性实例中的易失性成员函数 - 将数组分配给指针是无效转换?
- c++ - C ++ 17在逗号上拆分constexpr字符串并在编译时有元素数量?
- endpoint - 如何扩展现有的执行器健康检查端点?
- javascript - 尝试将数据存储在点击器网站中