csharp

Rename a Image/File in C#

In this article, we will learn how to Rename an Image/File in C#.

Listview: It is a control it is basically used for display item in different views like Large, Small, list, Tile and Details.
Complete Demo:
we will learn to a Rename Image/Replace old Image Name from New Image Name in c# (Winforms)Windows Forms.
Step 1.New project–>”Visual C# Windows”–>”Windows Form Application” .Write the name of a project like “ImagegalleryListView”.

Step 2. Open Form1 Design page drop one listView, one Button.Design your page like the screenshot.

Step 3.Double Click on Open Button “button_click” event generated. After that simply put this code on your form1.cs page.
Code For Rename Image/File in C#//Change//Replace Old Image Name From new Image name in C#
[php]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace ImageGalleryListView
{
public partial class Form1: Form
{

//Dynamically create Image List at runtime
System.Windows.Forms.ImageList myImageList = new ImageList();
//declare one int type variable count for index of image
int count = 0;
//Declare one string Variable for store New Image Name
public string ImageNewName="";
OpenFileDialog ofd = new OpenFileDialog() { Multiselect = true, ValidateNames = true, Filter =
"JPG|*jpg|JPEG|*.jpeg|PNG|*.png" };

public Form1()
{
InitializeComponent();
listViewFile.SmallImageList = myImageListSmall;
listViewFile.SmallImageList = myImageListLarge;
this.KeyPreview = true;
}

private void buttonOpen_Click(object sender, EventArgs e)
{
//set the size of imagelist
myImageList.ImageSize = new Size(50,50);
myImageListSmall.ImageSize = new Size(32, 32);
myImageListLarge.ImageSize = new Size(80, 80);
if (ofd.ShowDialog() == DialogResult.OK)
{
listViewFile.Items.Clear();
foreach (string fileName in ofd.FileNames)
{

FileInfo fi = new FileInfo(fileName);
FileInfo fileinfo = new FileInfo(fileName);
using (FileStream stream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read))
{
myImageList.Images.Add(Image.FromStream(stream));
}
listViewFile.LargeImageList = myImageList;

listViewFile.Items.Add(new ListViewItem { ImageIndex = count, Text = fi.Name, Tag = fi.FullName
});
count++;
}
}

}

/// <summary>
/// Here i have create dynamically shortcut menu Contextmenu
/// Add New menuItem "Rename"
/// Create OnMenuItemClick event on MenuItem Click
///
/// </summary>

private void listViewFile_MouseClick(object sender, MouseEventArgs e)
{

ContextMenu cm = new ContextMenu();
listViewFile.ContextMenu = cm;
var mi = new MenuItem("Rename");

mi.MenuItems.Add(mi);
mi.Click += OnMenuItemClick;
cm.MenuItems.Add(mi);

}
//Rename Image Name
//Don’t Forget to listview LabelEdit property allow to true by default labelEdit property false
private void OnMenuItemClick(object sender, EventArgs e)
{
listViewFile.SelectedItems[0].BeginEdit();
}

/// <summary>
/// If you want change system image name write this code
/// Here i have to create event.Like Normal listvView AfterLabelEdit event.
/// you can use this event whenever you have to require some action/operation on selected item
///Here replace old image name from NewImage
/// you have to fired this event and perform some action
///store edit image in ImageNewName string Variable
/// whatever you are select item store in ListViewItem object
/// fileInfo.MoveTo replace old Image of system from New Image Name
/// </summary>

private void listViewFile_AfterLabelEdit(object sender, System.Windows.Forms.LabelEditEventArgs e)
{
if (e.Label == null)
return;
ImageNewName = Convert.ToString(e.Label);
ListViewItem item = listViewFile.SelectedItems[0];
FileInfo fileinfo = new FileInfo(item.Tag.ToString());
fileinfo.MoveTo(fileinfo.Directory.FullName+"\\"+ImageNewName+fileinfo.Extension);
listViewFile.Items[0].Text = ImageNewName;
}
}
}

[/php]
Demo

1 thought on “Rename a Image/File in C#

Leave a Reply

Your email address will not be published.