I want to show the error occured, date, error description in a text file.
Following is vb code. Can any one tell me how to write it in vb.net????

Open App.Path & "\ErrorLog" & Replace(Date, "/", "_") & ".txt" For Append As #1
Write #1, "Error in News update Exe:- Form Load Function ---" & Err.Description & "---" & Time
Write #1, "--------------------------------"
Close #1


:'(

Dani AI

Generated

— converting that VB6 pattern to VB.NET is best done by creating a small reusable logger that (1) builds a safe filename, (2) ensures the log folder exists, and (3) appends a timestamped entry containing the full Exception text (use ex.ToString() to capture message + stack trace, not the old Err.Description). 's checklist is exactly the right learning path, and 's Try/Catch demonstrates the right idea; the notes below tighten it for reliability and deployment.

A compact helper you can drop into a WinForms app:

Public Sub LogError(ex As Exception, Optional context As String = "")
    Dim logDir = Path.Combine(Application.StartupPath, "ErrorLog")
    Directory.CreateDirectory(logDir)
    Dim filePath = Path.Combine(logDir, DateTime.Now.ToString("yyyy_MM_dd") & ".txt")
    Dim entry = DateTime.Now.ToString("o") & " - " & context & vbCrLf & ex.ToString() & vbCrLf & "----" & vbCrLf
    File.AppendAllText(filePath, entry)
End Sub

Key notes and troubleshooting:

  • Use Path.Combine to avoid manual backslashes and DateTime.Now.ToString("yyyy_MM_dd") for filenames (no slashes). See Path.Combine documentation.
  • Create the folder with Directory.CreateDirectory before writing to avoid IO errors. See Directory.CreateDirectory documentation.
  • On ASP.NET, write to ~/App_Data (use Server.MapPath or HostingEnvironment.MapPath) instead of the app install folder; writing under Program Files will fail under normal user accounts.
  • If writes fail, fallback to Event Log or a rolling log library. For concurrent access or production needs, prefer a logging framework (NLog/log4net) rather than ad hoc file writes.

These changes keep things robust across machines and deployments while preserving the simple file-based log you were using in VB6.

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

Learn how to:-

-Open a file for writing
-Write to a file
-Use string.replace to replace "/" with "_"
-Close the file

It appears that you want to use vb6 code in a Try:Catch block. You do need to learn the things stated above but try this:

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x, y As Integer
        y = 0
        Try
            x = 25 / y
        Catch ex As Exception
            Dim filename As String = My.Application.Info.DirectoryPath & "\ErrorLog" & Now.ToShortDateString.Replace("/", "_") & ".txt"
            Dim sw As StreamWriter = New StreamWriter(filename, True)
            sw.WriteLine("Error in News update Exe:- Form Load Function ---" & Err.Description & "---" & Now.ToLocalTime)
            sw.WriteLine("--------------------------------")
            sw.Flush()
            sw.Close()
            MessageBox.Show(filename)

        End Try

    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.