Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have written C# code to backup eventlog.It is returning a return code of 3.what does it mean? Please let me know if there is any error in my code or an alternate way to get a backup
Here is my code
C#
private void BackupEventLog()
{
    try
    {
        ManagementObjectSearcher searcher;
        ObjectQuery query;
        ConnectionOptions connection;
        ManagementScope scope = null;

        if ((txtUsername.Text != "") && (txtPassword.Text != ""))
        {
            connection = new ConnectionOptions();
            connection.Username = txtUsername.Text;
            connection.Password = txtPassword.Text;
            connection.Authority = "ntlmdomain:DOMAIN";

            scope = new ManagementScope("\\\\" + txtIP.Text + "\\root\\CIMV2", connection);
            scope.Connect();
        }
        else
        {
            scope = new ManagementScope();
        }
        scope.Options.EnablePrivileges = true;
        scope.Options.Impersonation = ImpersonationLevel.Impersonate;
        query = new ObjectQuery("Select * from Win32_NTEventLogFile Where LogFileName='Application'");
        searcher = new ManagementObjectSearcher(scope, query);

        foreach (ManagementObject o in searcher.Get())
        {
            ManagementBaseObject inParams = o.GetMethodParameters("BackupEventlog");
            inParams["ArchiveFileName"] = @"c:\scripts\Application.evt";
            ManagementBaseObject outParams=o.InvokeMethod("BackupEventLog", inParams, null);
            Response.Write(outParams.Properties["ReturnValue"].Value.ToString());
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}

Thank you in advance

[Added code formatting]
Posted
Updated 10-Mar-10 1:30am
v2

1 solution

Have a look at this:
Back Up and Clear an Event Log(Microsoft) [^]

Also a Code Snippet:
VB
Private Declare Function BackupEventLog Lib "advapi32.dll" Alias "BackupEventLogA" (ByVal hEventLog As IntPtr, ByVal lpBackupFileName As String) As Integer
Private Declare Function CloseEventLog Lib "advapi32.dll" (ByVal hEventLog As IntPtr) As IntPtr
Private Declare Function OpenEventLog Lib "advapi32.dll" Alias "OpenEventLogA" (ByVal lpUNCServerName As String, ByVal lpSourceName As String) As IntPtr

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim hEventLog As IntPtr
    Dim lretv As Integer
    hEventLog = OpenEventLog(vbNullString, "Application")
    If hEventLog = IntPtr.Zero Then
        System.Diagnostics.Debug.Write("OpenEvent Log Failed")
        Exit Sub
    End If
    lretv = BackupEventLog(hEventLog, "appback.evt")
    If lretv = 0 Then
        Debug.Write("BackupEventLog Failed")
        Exit Sub
    End If
End Sub
 
Share this answer
 
Comments
#realJSOP 14-Jul-10 7:51am    
This is a C# question, not VB.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900