How to Generate TextFile Dynamically and Read textFile in Asp.net

Description:-

Here we will create text File dynamically and Read text file from Generated text file in dot net using File Upload controls. First of all we will create new text file and we will create dynamically text file and folder in our directory where you want to create and using File Upload control through we will read text file in dot net.

Html Markup:-

<div>
   <div>
      <table>
         <tr>
            <td align="center" colspan="2">
               <asp:TextBox ID="textBox1" runat="server" TabIndex="0" Height="88px" TextMode="MultiLine" Width="495px"></asp:TextBox>
            </td>
         </tr>
         <tr>
            <td align="right">
               Enter path to store file:
            </td>
            <td>
               <asp:TextBox ID="txtpath" runat="server"></asp:TextBox>
            </td>
         </tr>
         <tr>
            <td>
            </td>
            <td>
               <asp:Button ID="btnCreate" runat="server" OnClick="btnCreate_Click" Text="Create" />
               <asp:Label ID="lbltxt" runat="server" ForeColor="Red"></asp:Label>
            </td>
         </tr>
         <tr>
            <td>
            </td>
            <td>
               <asp:FileUpload ID="fileupload1" runat="server" />
            </td>
         </tr>
         <tr>
            <td>
            </td>
            <td>
               <asp:Button ID="btnRead" runat="server" OnClick="btnRead_Click" Text="Read" />
            </td>
         </tr>
         <tr>
            <td valign="top">
               Message of Created Text file :
            </td>
            <td>
               <asp:TextBox ID="textBoxContents" runat="server" TabIndex="0" Height="176px" TextMode="MultiLine" Width="595px"></asp:TextBox>
            </td>
         </tr>
      </table>
   </div>
</div>

Here Code behind File we will create new Folder in D Drive for Store our Created File in dynamically and using File Upload Controls through we will read Generated text file. Here is the Code file.

Code behind:-

protected void btnCreate_Click(object sender, EventArgs e)
{
  string strMainFolderPath = @"D:\";
  //Create a new subfolder in the main folder.
  string newPath = System.IO.Path.Combine(strMainFolderPath, "GeneratedFile");
  DirectoryInfo objDirectory = new DirectoryInfo(newPath);
  if (!objDirectory.Exists)
  {
    System.IO.Directory.CreateDirectory(newPath);
  }
  string path = "D:\\GeneratedFile\\" + txtpath.Text + ".txt";
  if (!File.Exists(path))
  {
    string myString = textBox1.Text;
    string[] createText = myString.Split(new char[] { '\t' });
    File.WriteAllLines(path, createText);
  }
  string appendText = " " + Environment.NewLine;
  File.AppendAllText(path, appendText);
  lbltxt.Text = "File Created Successfully";
}

protected void btnRead_Click(object sender, EventArgs e)
{
  string path = fileupload1.PostedFile.FileName;
  string path1 = "D:\\GeneratedFile\\" + path;
  if (!string.IsNullOrEmpty(path))
  {
    string[] readText = File.ReadAllLines(path1);
    StringBuilder strbuild = new StringBuilder();
    foreach (string s in readText)
    {
      strbuild.Append(s);
      strbuild.AppendLine();
    }
    textBoxContents.Text = strbuild.ToString();
  }
}

Now run your Webpage in browser and Create Text File and Read text File.

Related Posts

Previous
Next Post »

Thanks for comments.....