我对您在创建.NET项目时可能在Program.cs中使用的任何常见例程/过程/方法感兴趣.例如,我通常在桌面应用程序中使用以下代码,以便轻松升级,单实例执行以及友好而简单的未捕获系统应用程序错误报告.
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
namespace NameoftheAssembly
{
internal static class Program
{
/// <summary>
/// The main entry point for the application. Modified to check for another running instance on the same computer and to catch and report any errors not explicitly checked for.
/// </summary>
[STAThread]
private static void Main()
{
//for upgrading and installing newer versions
string[] arguments = Environment.GetCommandLineArgs();
if (arguments.GetUpperBound(0) > 0)
{
foreach (string argument in arguments)
{
if (argument.Split('=')[0].ToLower().Equals("/u"))
{
string guid = argument.Split('=')[1];
string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
var si = new ProcessStartInfo(path + "\\msiexec.exe", "/x" + guid);
Process.Start(si);
Application.Exit();
}
}
//end of upgrade
}
else
{
bool onlyInstance = false;
var mutex = new Mutex(true, Application.ProductName, out onlyInstance);
if (!onlyInstance)
{
MessageBox.Show("Another copy of this running");
return;
}
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ThreadException += ApplicationThreadException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
var ex = (Exception) e.ExceptionObject;
MessageBox.Show("Whoops! Please contact the developers with the following"
+ " information:\n\n" + ex.Message + ex.StackTrace,
" Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
catch (Exception)
{
//do nothing - Another Exception! Wow not a good thing.
}
finally
{
Application.Exit();
}
}
public static void ApplicationThreadException(object sender, ThreadExceptionEventArgs e)
{
try
{
MessageBox.Show("Whoops! Please contact the developers with the following"
+ " information:\n\n" + e.Exception.Message + e.Exception.StackTrace,
" Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
catch (Exception)
{
//do nothing - Another Exception! Wow not a good thing.
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现这些例程非常有用.您在Program.cs中发现了哪些有用的方法?
我尽量避免在Program.cs文件中放入任何重要内容.我为一个简单的控制台应用程序编写的任何东西都可能有一天被移动到类库中,因此我将为实际的程序逻辑构建一个单独的类.
也就是说,有一些我一遍又一遍使用的通用代码.主要的是使用Trace类来处理控制台输出,所以我不必更改应用程序本身中的任何重要代码,以便在不可避免地转换到类库或gui app时将内容重定向到日志文件或其他地方.发生:
using System;
using System.Diagnostics;
public static void Main(string[] args)
{
Trace.Listeners.Add(new ConsoleTraceListener());
Trace.WriteLine("Program Started - " + DateTime.Now.ToString());Trace.WriteLine("");
//Call into a separate class or method for the actual program logic
DoSomething(); //I'll use Trace for output in here rather than Console
Trace.WriteLine("");Trace.WriteLine("Program Finished - " + DateTime.Now.ToString());
Console.Write("Press a key to exit...");
Console.ReadKey(true);
Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5961 次 |
| 最近记录: |