# How can I use Windows Form in a dotnet core Fsharp script?

**URL:** <https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296>\
**Category:** Uncategorized\
**Created:** [May 19, 2020, 5:53am UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296 "2020-05-19T05:53:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![systems](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/systems/32/585_2.png) [@systems](https://forums.fsharp.org/u/systems)\
**Post date:** [May 19, 2020, 5:53am UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/1 "2020-05-19T05:53:00Z")

</div>

How can I used Windows Form in a dotnet core Fsharp script?

`open System.Windows.Forms`

raise the warning or error that The namespace ‘Forms’ is not defined.

I understand that using Windows.Forms in dotnet need some work or special setup  
and there is a link for this [https://docs.microsoft.com/en-us/dotnet/core/porting/winforms](https://docs.microsoft.com/en-us/dotnet/core/porting/winforms)

But I couldnt find a way to declare this for an F# script

---

<div class="post-metadata">

**Author:** ![murphy](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/murphy/32/1373_2.png) [@murphy](https://forums.fsharp.org/u/murphy)\
**Post date:** [May 20, 2020, 1:06pm UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/2 "2020-05-20T13:06:20Z")

</div>

You will have to reference the DLLs providing the Windows Forms API.

One thing you could try is to run `fsi.exe` with the `--langversion:preview` switch, add a `#r "nuget: System.Windows.Forms"` to the top of your script, and keep your fingers crossed that the `open System.Windows.Forms` statement and further use of the API works 😉

Another option would be to use [Paket](https://fsprojects.github.io/Paket/) and generate a load script for the package that you could reference from your own script: Change to a fresh working directory, initialize Paket using `paket init`, add a reference to the Windows Forms package with `paket add System.Windows.Forms`, generate a load script with `paket generate-load-scripts -f netcore3.1 -t fsx`, add a `#load ".paket/load/netcoreapp3.1/System.Windows.Forms.fsx"` to the top of your script, and again hope for the best.

---

<div class="post-metadata">

**Author:** ![systems](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/systems/32/585_2.png) [@systems](https://forums.fsharp.org/u/systems)\
**Post date:** [May 20, 2020, 5:40pm UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/3 "2020-05-20T17:40:47Z")

</div>

First thanks for the reply and suggestions

I tried both options, I am getting the same error

> error FS0193: internal error: Could not load file or assembly ‘System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL’. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)

What does this mean 🤔

---

<div class="post-metadata">

**Author:** ![murphy](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/murphy/32/1373_2.png) [@murphy](https://forums.fsharp.org/u/murphy)\
**Post date:** [May 20, 2020, 6:57pm UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/4 "2020-05-20T18:57:51Z")

</div>

It seems the Windows Forms stuff is handled specially – the NuGet package is actually unlisted, contains only stub libraries apparently, and regular `.fsproj` files do not reference the package directly but rather set a magic flag `<UseWindowsForms>true</UseWindowsForms>` to enable Windows Forms support for a `netcoreapp3.*` target. I have no idea how to emulate this for an F# script 😕

---

<div class="post-metadata">

**Author:** ![systems](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/systems/32/585_2.png) [@systems](https://forums.fsharp.org/u/systems)\
**Post date:** [May 20, 2020, 7:25pm UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/5 "2020-05-20T19:25:15Z")

</div>

Thanks 🙂

I also notice, the dotnet command does support or doesnt have an F# winforms template  
But I think I can start with a console app and add it as package

I will start from there

---

<div class="post-metadata">

**Author:** ![systems](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/systems/32/585_2.png) [@systems](https://forums.fsharp.org/u/systems)\
**Post date:** [May 22, 2020, 1:13pm UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/6 "2020-05-22T13:13:28Z")

</div>

Ok, I cant find how to create an winforms app in F# in dotnet core  
Is this at all possible?  
Why is it so complicated? why winforms isnt just another package or library that you can add, reference and then use?

I am asking, because I think it will help me understand the platform better, winforms seem special and it even seem to have special flags in the project file

```auto
<UseWindowsForms>true</UseWindowsForms>

```

Why is all of this needed?  
And finally how can I do it in an F# template although dotnet core doesnt have a template for it

---

<div class="post-metadata">

**Author:** ![Tarmil](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/tarmil/32/259_2.png) [@Tarmil](https://forums.fsharp.org/u/Tarmil)\
**Post date:** [May 22, 2020, 3:39pm UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/7 "2020-05-22T15:39:32Z")

</div>

The assemblies are actually included in the .NET Core SDK, rather than via NuGet. Same with [ASP.NET](http://ASP.NET) Core, actually. On my machine they’re located in the folder `C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\3.1.4\`. I haven’t tried to reference them from fsi though, not sure if it’s enough.

---

<div class="post-metadata">

**Author:** ![systems](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/systems/32/585_2.png) [@systems](https://forums.fsharp.org/u/systems)\
**Post date:** [May 27, 2020, 3:47am UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/8 "2020-05-27T03:47:52Z")

</div>

Ok so I finally got listing 4.8 from Get Programming with F#

My project file look like this

```
> <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
> 
> <PropertyGroup>
> <OutputType>WinExe</OutputType>
> <TargetFramework>netcoreapp3.1</TargetFramework>
> <UseWindowsForms>true</UseWindowsForms>
> </PropertyGroup>
>
> <ItemGroup>
> <Compile Include="Program.fs" />
> </ItemGroup>
> </Project>

```

I think the key line was specifying the correct Sdk  
As I am a beginner in F#, I still dont know much abut the diffeent SDKs why they exists , and which one to use one etc …

I also dont know why this entry was needed, what does it do  
`<UseWindowsForms>true</UseWindowsForms>`

---

<div class="post-metadata">

**Author:** ![Palmer](https://avatars.discourse-cdn.com/v4/letter/p/d78d45/32.png) [@Palmer](https://forums.fsharp.org/u/Palmer)\
**Post date:** [September 17, 2020, 8:00am UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/9 "2020-09-17T08:00:31Z")

</div>

This option worked for me.

```
#I @"C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\3.1.7"
#r "System.Windows.Forms"

```

The first line #I specifies the directory path the search for the .dll, the #r specifies the .dll to reference.

---

<div class="post-metadata">

**Author:** ![theundergroundsorcer](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@theundergroundsorcer](https://forums.fsharp.org/u/theundergroundsorcer)\
**Post date:** [December 19, 2020, 7:58pm UTC](https://forums.fsharp.org/t/how-can-i-use-windows-form-in-a-dotnet-core-fsharp-script/1296/10 "2020-12-19T19:58:16Z")

</div>

@Palmer  
When trying use this solution from .net 5 - after referencing the relevant dlls, I end up with someting like " “Could not load file or assembly ‘System.Drawing.Common, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’. Could not find or load a specific file. (0x80131621)”.

Can you confirm that it works under .net 5? Did it work under .net 3.1 at the end?  
Thanks.
