Hi nauna,
Refer below example.
HTML
<asp:RadioButtonList ID="rblPages" runat="server">
</asp:RadioButtonList>
<br />
<asp:Button ID="btnRename" Text="Rename" runat="server" OnClick="Rename" />
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindPages();
}
}
protected void Rename(object sender, EventArgs e)
{
string pageName = rblPages.SelectedItem.Text;
string path = Server.MapPath("~/Pages/");
File.Copy(Server.MapPath("~/Pages/") + pageName + ".aspx", Server.MapPath("~/Pages/") + pageName + "_changed.aspx");
File.Copy(Server.MapPath("~/Pages/") + pageName + ".aspx.cs", Server.MapPath("~/Pages/") + pageName + "_changed.aspx.cs");
File.Delete(Server.MapPath("~/Pages/") + pageName + ".aspx");
File.Delete(Server.MapPath("~/Pages/") + pageName + ".aspx.cs");
this.BindPages();
}
private void BindPages()
{
string[] pages = Directory.GetFiles(Server.MapPath("~/Pages"), "*.aspx");
DataTable dt = new DataTable();
dt.Columns.Add("PageName");
foreach (string page in pages)
{
dt.Rows.Add(Path.GetFileNameWithoutExtension(page));
}
rblPages.DataSource = dt;
rblPages.DataTextField = "PageName";
rblPages.DataValueField = "PageName";
rblPages.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindPages()
End If
End Sub
Protected Sub Rename(ByVal sender As Object, ByVal e As EventArgs)
Dim pageName As String = rblPages.SelectedItem.Text
Dim path As String = Server.MapPath("~/Pages/")
File.Copy(Server.MapPath("~/Pages/") & pageName & ".aspx", Server.MapPath("~/Pages/") & pageName & "_changed.aspx")
File.Copy(Server.MapPath("~/Pages/") & pageName & ".aspx.vb", Server.MapPath("~/Pages/") & pageName & "_changed.aspx.vb")
File.Delete(Server.MapPath("~/Pages/") & pageName & ".aspx")
File.Delete(Server.MapPath("~/Pages/") & pageName & ".aspx.vb")
Me.BindPages()
End Sub
Private Sub BindPages()
Dim pages As String() = Directory.GetFiles(Server.MapPath("~/Pages"), "*.aspx")
Dim dt As DataTable = New DataTable()
dt.Columns.Add("PageName")
For Each page As String In pages
dt.Rows.Add(Path.GetFileNameWithoutExtension(page))
Next
rblPages.DataSource = dt
rblPages.DataTextField = "PageName"
rblPages.DataValueField = "PageName"
rblPages.DataBind()
End Sub