WPF window throws TypeInitializationException at start up

江枫思渺然 提交于 2019-12-01 06:23:09

Judging from the error stack you posted, it appears that your settings window probably has a link on it. Unfortunately, the link has an invalid url in it, which causes the creation of the window to fail.

EDIT: Having looked more closely at your code and error messages, it's clear that I was looking in the wrong area. Some searching turned up a bug in wpf.

The short version: the windir environment variable may be set incorrectly on the target machine, which causes the font subsytem to break. The workaround is to fix the environment variable, either on the target machine's registry, or in the code by adding something like the following to the startup:

Environment.SetEnvironmentVariable("windir", Environment.GetEnvironmentVariable("SystemRoot"));
David Gardiner

We had a couple of people experience this problem. The solution described here (caused by PATH being greater than 2048 characters) resolved the issue.

I am concerned that the statement suggested above may be problematic in code that is running with either too many or too few privileges. Certainly my app running with on my workstation with me as administrator has no issue with this call to Environment.SetEnvironmentVariable("windir",Environment.GetEnvironmentVariable("SystemRoot"));

but I am not sure that SetEnvironmentVariable will actually work on a locked-down OS. Has anybody a) tried this for real and b) tried it in a low-privilege environment>

Also, I am seeing some oddity on app startup after I install an app with this call, and run it once. All other applications no longer can see windir,which is not expanding to C:\Windows. I am going to go through the whole install and run without it in place and see if either the TypeInvocation exception resurfaces or if not, whether the OS windir variable is left intact.

UPDATE:

I have confirmed that the bad behavior I noticed was due to not specifying the target of the SetEnvironment call. Using the following my problems seem to have been alleviated:

System.Environment.SetEnvironmentVariable("windir", System.Environment.GetEnvironmentVariable("SystemRoot"), EnvironmentVariableTarget.User);

When I did not set the target, very odd things happened to my Windows session requiring me to either logoff and login or even reboot. So unless this change is really to be at the machine or process level, do not assume happy results with the default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!