Solving the error: MyApplication.exe has encountered a problem and needs to close. We are sorry for the inconvenience in a C# application

I was getting the error: MyApplication.exe has encountered a problem and needs to close.  We are sorry for the inconvenience, but I was not getting any unhandled exceptions in my “HandleGlobalExceptions” event:   AppDomain.CurrentDomain.UnhandledException += HandleGlobalExceptions;   After setting some MessageBox.Show("Step 1") statements, I could see that the application was …

Extract contents of msi packages in C#

If you want to extract the contents of a msi package to filesystem, you can use the following C# function:   public void ExtractMsiPackage() {    string parameters = string.Empty; parameters = string.Format(@"/a {0} /qb TARGETDIR=""{1}"" REINSTALLMODE=amus", "C:\Temp\Test.msi", "C:\Temp\Extract"); Process process = Process.Start("msiexec", parameters); process.WaitForExit(); } /a = Administrative mode /qb …

Find and Replace text in all files of a given folder , including subfolders with C#

If you want to find and replace some text in all files of a given folder, including subfolders, you can use the following C# code: string rootfolder = @"C:\Temp"; string[] files = Directory.GetFiles(rootfolder, "*.*", SearchOption.AllDirectories); foreach (string file in files) {    try {    string contents = File.ReadAllText(file); contents = contents.Replace(@"Text …

How to run an existing SQL Server Agent Job from C#

If you want to execute an existing Microsoft SQL Server Agent Job in C# you an use the following function: The function uses SQL authentication to connect to the Microsoft SQL Server instance. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Smo.Agent; using Microsoft.SqlServer.Management.Common; using System.Data;   …

Showing your log4net logging in your C# application

If you want to show the log4net logging direct in your application by using a textbox, you can use the following link: http://weblogs.asp.net/psteele/archive/2010/01/25/live-capture-of-log4net-logging.aspx I tried it and it works great:   using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using log4net.Appender; using …