Hey Bharath143,
Please refer below sample.
HTML
<div>
FileName:
<asp:TextBox runat="server" ID="txtFileName" />
<asp:Button Text="Create" runat="server" OnClick="Create" />
</div>
Namespaces
C#
using System.IO;
using System.Web.Hosting;
VB.Net
Imports System.IO
Imports System.Web.Hosting
Code
C#
protected void Create(object sender, EventArgs e)
{
// aspx
string aspxPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "CS.aspx");
string[] aspxContents = File.ReadAllLines(aspxPath);
List<string> aspx = new List<string>();
for (int i = 0; i < aspxContents.Length; i++)
{
aspx.Add(aspxContents[i].Replace("CodeFile=\"CS.aspx.cs\"", "CodeFile=\"" + txtFileName.Text + ".aspx.cs\"").Replace("Inherits=\"CS\"", "Inherits=\"" + txtFileName.Text + "\""));
}
// cs
string csPath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "CS.aspx.cs");
string[] csContents = File.ReadAllLines(csPath);
List<string> cs = new List<string>();
for (int i = 0; i < csContents.Length; i++)
{
cs.Add(csContents[i].Replace("class CS", "class " + txtFileName.Text));
}
File.WriteAllLines(Server.MapPath("~/" + txtFileName.Text + ".aspx"), aspx.ToArray());
File.WriteAllLines(Server.MapPath("~/" + txtFileName.Text + ".aspx.cs"), cs.ToArray());
Response.Redirect(txtFileName.Text + ".aspx");
}
VB.Net
Protected Sub Create(ByVal sender As Object, ByVal e As EventArgs)
Dim aspxPath As String = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "CS.aspx")
Dim aspxContents As String() = File.ReadAllLines(aspxPath)
Dim aspx As List(Of String) = New List(Of String)()
For i As Integer = 0 To aspxContents.Length - 1
aspx.Add(aspxContents(i).Replace("CodeFile=""CS.aspx.cs""", "CodeFile=""" & txtFileName.Text & ".aspx.cs""").Replace("Inherits=""CS""", "Inherits=""" & txtFileName.Text & """"))
Next
Dim csPath As String = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "CS.aspx.cs")
Dim csContents As String() = File.ReadAllLines(csPath)
Dim cs As List(Of String) = New List(Of String)()
For i As Integer = 0 To csContents.Length - 1
cs.Add(csContents(i).Replace("class CS", "class " & txtFileName.Text))
Next
File.WriteAllLines(Server.MapPath("~/" & txtFileName.Text & ".aspx"), aspx.ToArray())
File.WriteAllLines(Server.MapPath("~/" & txtFileName.Text & ".aspx.cs"), cs.ToArray())
Response.Redirect(txtFileName.Text & ".aspx")
End Sub
Screenshot
