//Please Fill free to ask query about this code,in case of error or have some changes to make will be accepted.
//Thank you for using this code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; //Use this Namespace.
namespace ClientOM
{
public partial class ExtnChanger : Form
{
public ExtnChanger()
{
InitializeComponent();
}
private void btnclear_Click(object sender, EventArgs e)
{
txtpath.Text = string.Empty;
txtnewext.Text = string.Empty;
lblmsg.Text = string.Empty;
}
private void btnchngextn_Click(object sender, EventArgs e)
{
if (txtpath.Text == string.Empty || txtnewext.Text == "")
{
lblmsg.Text = string.Empty;
MessageBox.Show("Directory Path/File Extension should not Empty.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
try
{
string dirpath = txtpath.Text.ToString();
string[] filepath = Directory.GetFiles(dirpath);
foreach (string myfile in filepath)
{
string filename = Path.ChangeExtension(myfile, txtnewext.Text);
System.IO.File.Move(myfile, filename);
}
MessageBox.Show("File Extension has been changed successfully.", "Success", MessageBoxButtons.OK);
lblmsg.Text = string.Empty;
}
catch (Exception ex)
{
MessageBox.Show("Invalid Directory/Filename!", "Error", MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
}
// To Validate TextBox
private void txtpath_Leave(object sender, EventArgs e)
{
try
{
string dirpath = txtpath.Text.ToString();
string[] filepath = Directory.GetFiles(dirpath);
lblmsg.Text = "Directory Contains '" + filepath.Length + "' files inside.";
}
catch (Exception Ex)
{
lblmsg.Text = "Invalid Directory/Filename!";
}
}
// To Validate Label
private void txtnewext_Leave(object sender, EventArgs e)
{
lblmsg.Text = string.Empty;
}
}
}You can change Multiple File Extensions at a time. Extension should be same for all file,which are going to changed.
