如何避免FileNotFoundException异常,如果.NET 3.5没有安装?没有安装、异常、FileNotFoundException、NET

2023-09-03 21:28:50 作者:你是我的心脏

如果您尝试启动一个.NET 3.5在Windows计算机上的应用程序,它没有这个版本安装了.NET框架,你会得到一个 FileNotFoundException异常对于一些系统组件(例如System.Core程序3.5.0.0)。

If you try to launch a .NET 3.5 application on a Windows computer which does not have this version of the .NET framework installed, you get a FileNotFoundException for some system assemblies (for example System.Core 3.5.0.0).

是否有可能赶上这个异常,并告诉用户升级他们的.NET框架,抑或是抛出过早地处理呢?

Is it possible to catch this exception and tell the user to upgrade their .NET framework or is it thrown too early to handle it?

推荐答案

你是如何正在部署的应用程序?的ClickOnce可以做组件(GAC)检查发射前,并与微星,你应该有一个可用的一整套pre-检查选项...虽然并不总是可行的,你可能会考虑这些部署选项?

How are you currently deploying the app? ClickOnce can do assembly (GAC) checks before launch, and with msi you should have a whole range of pre-check options available... although not always feasible, you might consider one of these deployment options?

重新捕捉异常 - 只是一定要拆主了,这样它的没有的只是捕捉到了异常 - 否则JIT可以阻止你的主加载:

Re catching the exception - just be sure to split the Main up so that it does nothing except catch the exception - otherwise JIT can stop your Main from loading:

// [STAThread] here if winform
[MethodImpl(MethodImplOptions.NoInlining)]
static void Main() {
    try {
       MainCore();
    } catch (SomeException ex) {
       // TODO something simple but fun
    }
}

static void MainCore() { ... } // your app here...

如果你把太多的外主,它可以运行的任意的的前BARF,因为JIT可能需要的类型。

If you put too much in the outer Main, it can barf before running any of it, since JIT might need the types.