我使用Visual Studio 2013(目标.net 3.5 SP1)开发了一个C#winform应用程序。我将应用程序发布为ClickOnce。应用程序已成功安装在Windows XP上,但在安装后尝试立即运行时,会发生以下错误:
对象引用未设置为对象的实例
我在互联网上查看但仍无法找到解决此问题的方法。我也问了这个问题here,但还没有得到答案。
修改
我没有在调试模式(IDE)中收到错误。我在部署应用程序时遇到错误,因此我不知道代码在哪里发出错误。我怎么知道问题是什么?我还检查了Windows事件查看器,我怀疑在事件查看器中发现了一个错误。事件查看器中的错误描述是
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。
这是我的program.cs文件:
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);
}
}
}