dotnet / dotnet/winforms

SendKeys.SendWait will throw could not load type OsVersion exception

Open
#14,145 3 comments 0 reactions 0 assignees View on GitHub
needs-area-label untriaged
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
20h 23m
Merged PRs (30d)
103

Description

### .NET version

.NET 10/.NET 9

### Did it work in .NET Framework?

Not tested/verified

### Did it work in any of the earlier releases of .NET Core or .NET 5+?

None.

### Issue description

When use `` in .NET 9 TargetFramework, the `System.Windows.Forms.SendKeys.SendWait("Hello, World!");` will throw the exception:

```
System.TypeInitializationException: The type initializer for 'System.Windows.Forms.SendKeys' threw an exception.
---> System.TypeInitializationException: The type initializer for 'System.Windows.Forms.ScaleHelper' threw an exception.
---> System.TypeLoadException: Could not load type 'System.Private.Windows.Core.OsVersion' from assembly 'System.Private.Windows.Core, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
at System.Windows.Forms.ScaleHelper.g__GetPerMonitorAware|8_1()
at System.Windows.Forms.ScaleHelper.InitializeStatics()
--- End of inner exception stack trace ---
at System.Windows.Forms.ScaleHelper.get_InitialSystemDpi()
at System.Windows.Forms.Control..ctor(Boolean autoInstallSyncContext)
at System.Windows.Forms.SendKeys.SKWindow..ctor()
at System.Windows.Forms.SendKeys..cctor()
--- End of inner exception stack trace ---
at System.Windows.Forms.SendKeys.SendWait(String keys)
at sendkey_error.MainWindow.Button_Click(Object sender, RoutedEventArgs e)
```

This issue origin reported in https://github.com/dotnet/wpf/issues/11313

And I think it was introduced by https://github.com/dotnet/winforms/pull/12839

Cc @JeremyKuhne

### Steps to reproduce

1. Create a simple .NET project with the .csproj:

```xml


Exe
net9.0-windows
enable
enable
true



```

2. Call the `SendKeys.SendWait` in Program.cs

```csharp
System.Windows.Forms.SendKeys.SendWait("Hello, World!");
```

3. Build and run the project and you can find the exception.

You can find all my demo code in https://github.com/lindexi/lindexi_gd/tree/1534db9e5f807d2c958cc8f7509f9f45229651e5/WPFDemo/LaiwerelawkewaFeereajemle

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.