You must Sign In to post a response.
  • Category: .NET

    How to save the data in notepad in VB.NET?

    How to save the data in notepad in VB.NET ?
    aspdotnet-roja
  • #724985
    Hi.
    You can use the following code to save the data in notepad in vb.net:
    Dim data As String = "D:\dataFile.txt"
    IO.File.WriteAllText(data, TextBox1.Text)

    Hope it helps.

    Thanks

    Reena

  • #724990
    Here is a sample code which write exception in note paf file.


    private static bool WriteErrorLog(string strPathName,Exception ex)
    {
    bool bReturn = false;
    string strEx = "";
    try
    {
    sw = new StreamWriter(strPath,true);
    sw.WriteLine("Source : " + ex.Source.ToString().Trim());
    sw.WriteLine("Method : " +
    ex.TargetSite.Name.ToString());
    sw.WriteLine("Date : " +
    DateTime.Now.ToLongTimeString());
    sw.WriteLine("Time : " +
    DateTime.Now.ToShortDateString());
    sw.WriteLine("Computer : " +
    Dns.GetHostName().ToString());
    sw.WriteLine("Error : " +
    ex.Message.ToString().Trim());
    sw.WriteLine("Stack Trace : " +
    ex.StackTrace.ToString().Trim());
    sw.WriteLine("*******");
    sw.Flush();
    sw.Close();
    bReturn = true;
    }
    catch(Exception)
    {
    bReturn = false;
    }
    return bReturn;
    }

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #725077
    This given code can be use in VB.NET for save the data in notepad under the button click event
    Imports System.IO
    Public Class Form2
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim myvar As StreamWriter
    myvar= File.AppendText("c:\temp\mynotepadfile.txt")
    myvar.WriteLine(TxtBox3.Text & " " & TxtBox4.Text)
    myvar.Close()
    End Sub
    End Class
    I hope above example may be useful for you answer

  • #725128
    Refer below detailed code understand how to write and read lines in file


    Imports System.IO

    Public Partial Class Form1
    Inherits Form
    Public Sub New()
    InitializeComponent()
    End Sub
    'Below code is used to write linse in your file
    Private Sub button1_Click(sender As Object, e As EventArgs)
    Dim txt As String = "Hi How are you Welcome to My World"
    Dim sw As StreamWriter
    Try
    sw = File.CreateText("c://test.txt")
    sw.WriteLine(txt)
    sw.Close()
    MessageBox.Show("Write Successfully")
    Catch ex As Exception
    MessageBox.Show(ex.ToString())
    End Try
    End Sub
    'Below code is used to read your file line by line
    Private Sub button2_Click(sender As Object, e As EventArgs)
    Dim sr As StreamReader
    Try
    sr = File.OpenText("c://test.txt")
    label1.Text = sr.ReadToEnd()
    'In the above line you can use find option then replace word then write to another file
    sr.Close()
    MessageBox.Show(label1.Text.ToString())
    Catch ex As Exception
    MessageBox.Show(ex.ToString())
    End Try
    End Sub
    End Class

    Regards
    N.Ravindran
    Your Hard work never fails

  • #725300
    If you are interest to save the data in notepad in C#.NET then I recommended below code

    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.IO;

    Public Class Form2
    {

    private void Btn1_Click(System.Object sender, System.EventArgs e)
    {
    StreamWriter swtr = null;
    swtr = File.AppendText("D:\\temp\\yourfile.txt");
    swtr.WriteLine(TextBox2.Text + " " + TextBox3.Text);
    swtr.Close();
    }
    }


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.