Hi nirmal,
Please refer below sample.
HTML
<asp:Button Text="Split File" ID="btnSplit" OnClick="SpitDataFiles" runat="server" />
Namespaces
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
protected void SpitDataFiles(object sender, EventArgs e)
{
string[] lines = File.ReadAllLines(Server.MapPath("~/Files/DataFiles.txt"));
string fileData = string.Empty;
for (int i = 0; i < lines.Length; i++)
{
string fileName = Server.MapPath("~/Files/%DataFiles" + (i + 1) + "%.txt");
string line = lines[i];
if (i == 0)
{
fileData += line + ("\n");
}
else if (line.StartsWith("FILE") && i != 0)
{
File.WriteAllText(fileName, fileData);
fileData = string.Empty;
fileData += line + ("\n");
}
else
{
fileData += line + "\n";
}
if (lines.Length == i + 1)
{
File.WriteAllText(fileName, fileData);
}
}
}
VB.Net
Protected Sub SpitDataFiles(ByVal sender As Object, ByVal e As EventArgs)
Dim lines As String() = File.ReadAllLines(Server.MapPath("~/Files/DataFiles.txt"))
Dim fileData As String = String.Empty
For i As Integer = 0 To lines.Length - 1
Dim fileName As String = Server.MapPath("~/Files/%DataFiles" & (i + 1) & "%.txt")
Dim line As String = lines(i)
If i = 0 Then
fileData += line & (vbLf)
ElseIf line.StartsWith("FILE") AndAlso i <> 0 Then
File.WriteAllText(fileName, fileData)
fileData = String.Empty
fileData += line & (vbLf)
Else
fileData += line & vbLf
End If
If lines.Length = i + 1 Then
File.WriteAllText(fileName, fileData)
End If
Next
End Sub
Screenshot