通过单击重命名文件/文件夹(在自制窗口资源管理器中使用列表视图)
本文关键字:资源管理器 窗口 视图 列表 重命名 单击 文件 文件夹 | 更新日期: 2023-09-27 17:55:31
每当
我单击文件夹时,我想在运行时在我的窗口资源管理器中重命名文件夹/文件...到目前为止,我知道File.move用于重命名,但是如何在运行时输入文件名?
private void renameToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string DestinationFolder = ListviewCurrentFolderPath;
string CurrentName = DestinationFolder + "FileName";//in filename i want to edit value on run time
if (Directory.Exists(DestinationFolder))
{
// Directory.Move( Path.Combine(new string[] { DestinationFolder,Path.GetFileName(file) }));
File.Move( Path.Combine(new string[] { DestinationFolder, CurrentName }));
PopulateListView(DestinationFolder);
}
}
catch (IOException ios)
{
MessageBox.Show(ios.ToString());
}
}
我猜你的意思是用"运行时重命名"来重命名列表视图项目的标签,这会影响重命名文件夹......
所以,首先你应该看看
- ListView.LabelEdit - 启用"重命名"
- ListView.BeforeLabelEdit
- ListView.AfterLabelEdit
我希望这个小样本能帮助你找到正确的方法:)
using System;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
private string destinationFolder;
private ListView listView1;
public Form1()
{
// Set destinationFolder to MyDocuments - for test
this.destinationFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
InitializeComponent();
this.listView1.Clear();
foreach (var dir in Directory.GetDirectories(destinationFolder))
{
this.listView1.Items.Add(new ListViewItem() { Name = dir, Text = Path.GetFileName(dir) });
}
}
private void SetListViewItemName(int index, string name)
{
if (this.listView1.Items.Count < index)
{
this.listView1.Items[index].Name = name;
}
}
private string GetListViewItemText(int index)
{
if (this.listView1.Items.Count < index)
{
return this.listView1.Items[index].Text;
}
else
{
return String.Empty;
}
}
private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
try
{
string itemText = GetListViewItemText(e.Item);
string sourceDirName = Path.Combine(new string[] { this.destinationFolder, itemText });
string destDirName = Path.Combine(new string[] { this.destinationFolder, e.Label });
// Rename the old directory.
Directory.Move(sourceDirName, destDirName);
SetListViewItemName(e.Item, destDirName);
}
catch (Exception ex)
{
// Error occured, cancel edit.
// Empty text, cancel edit.
// There are few more things to check: max pathlength, invalid chars etc.
e.CancelEdit = true;
}
}
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.SuspendLayout();
//
// listView1
//
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.LabelEdit = true;
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(284, 262);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.List;
this.listView1.AfterLabelEdit += new System.Windows.Forms.LabelEditEventHandler(this.listView1_AfterLabelEdit);
//this.listView1.BeforeLabelEdit += new System.Windows.Forms.LabelEditEventHandler(this.listView1_BeforeLabelEdit);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.ResumeLayout(false);
}
}
}
可能需要使用 SaveFileDialog 类。您可以使用 FileDialog.InitialDirectory 属性的DestinationFolder
。