gpt4 book ai didi

c# - 您在 C# 的 Program.cs 中放入了哪些常用例程

转载 作者:太空狗 更新时间:2023-10-29 18:30:32 29 4
gpt4 key购买 nike

我对您在创建 .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.
}
}
}
}

我发现这些例程非常有用。您发现哪些方法对 Program.cs 有帮助?

最佳答案

我尽量避免在 Program.cs 文件中放置任何重要内容。我为一个简单的控制台应用程序编写的任何东西有一天都可以移到类库中,因此我将为实际的程序逻辑构建一个单独的类。

就是说,有一些我反复使用的通用代码。主要是使用 Trace 类来处理控制台输出,因此当不可避免地过渡到类库或 gui 应用程序时,我不必更改应用程序本身的任何重要代码以将内容重定向到日志文件或其他地方发生:

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();
}

关于c# - 您在 C# 的 Program.cs 中放入了哪些常用例程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1667764/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com