I developed a C# winform application using Visual Studio 2013 (targeted .net 3.5 SP1). I published the application as ClickOnce. The application is installed on windows XP successfully, but when it tries to run immediately after installation, the following error occurs:
object reference not set to an instance of an object
I looked on the internet but still can not find a solution to this problem. I also asked this question here, but didn't get answer yet.
Edit:
I don't get the error in debug mode (IDE). I get the error when I deploy the application so I don't know where in the code the error is issued. How can I know what the problem is? I also checked the windows Event Viewer and I suspect one of the errors I found in the Event Viewer. The description of the error in the Event Viewer is
EventType clr20r3, P1 applaunch.exe, P2 2.0.50727.3053, P3 4889dc54, P4 mscorlib, P5 2.0.0.0, P6 4889dc80, P7 4f2, P8 0, P9 system.security.security, P10 NIL.
Here is my program.cs file:
using SoftwareLocker;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Security.Principal;
using System.Windows.Forms;
namespace MyApplicationNamespace
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
// Check if user is NOT admin
if (!IsRunningAsAdministrator())
{
// Setting up start info of the new process of the same application
ProcessStartInfo processStartInfo = new ProcessStartInfo(Assembly.GetEntryAssembly().CodeBase);
// Using operating shell and setting the ProcessStartInfo.Verb to “runas” will let it run as admin
processStartInfo.UseShellExecute = true;
processStartInfo.Verb = "runas";
// Start the application as new process
Process.Start(processStartInfo);
// Shut down the current (old) process
System.Windows.Forms.Application.Exit();
return;
}
Application.Run(new frmEnLogin());
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(e.ExceptionObject.ToString());
}
private static bool IsRunningAsAdministrator()
{
// Get current Windows user
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
// Get current Windows user principal
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);
// Return TRUE if user is in role "Administrator"
return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}