C
C#2y ago
uselessxp

✅ Clipboard.SetText in a ConsoleApp

Well, as a little task I'm trying to code a ConsoleApp that should base64 encrypt an input string and automatically copy it to the clipboard. The ideal result is the following, by cmd: - -base64.exe -e string (for encrypt) - -base64.exe -d string (for decrypt) First of all I'm playing with encoding/decoding and I figured out that I have to convert the input string into a byte (array?), then after I can apply the method that convert to base64. What I'm unable to do, and that I always did in VB.NET Winforms, is Clipboard.SetText and I'm surprised to know that's strictly related to WinForms, since we're not talking abuot a button, or a textbox, but about the clipboard, that's global and abstract. Btw from the Microsoft documentation (I'm trying to understand this one also if harder) I assumed two informations: Namespace: System.Windows.Forms Assembly: System.Windows.Forms.dll So as what I learned, I tried those steps: 1) add
using System.Windows.Forms;
using System.Windows.Forms;
2) add a COM reference to System.Windows.Forms I don't know if I thought in the right way, but this seems not working.
22 Replies
ero
ero2y ago
the clipboard class is strictly windows-only that's why it's in System.Windows.Forms it's honestly just unexpected UX to just copy something to a user's clipboard don't do that
uselessxp
uselessxpOP2y ago
oh, ok. when I discovered this ability I found it cool and I used it more times btw just for curiosity, how could I fix it and why my solution don't works? what do you mean for clip?
ero
ero2y ago
you need to specifically enable <UseWindowsForms>true</UseWindowsForms> in your project file and set your target framework to net7.0-windows (or whatever .net version you're using)
uselessxp
uselessxpOP2y ago
how could I know myselft that a COM reference can't be flagged and imported normally, without this additional step?
ero
ero2y ago
i don't even know what in the world a COM reference is never used it ever
uselessxp
uselessxpOP2y ago
the same, I suppose is when you add a not external reference, but "an already existing one"
ero
ero2y ago
looks like COM is legacy don't use it
uselessxp
uselessxpOP2y ago
oh, well^^
uselessxp
uselessxpOP2y ago
it doesn't want works anyway, do you think it would be possible to do it in another way? I know I should not set text into clipboard, but I'd just like to know how to do it.
ero
ero2y ago
of course there's other ways .net7 windows forms apps exist
uselessxp
uselessxpOP2y ago
I mean to do it on a ConsoleApp project
ero
ero2y ago
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>

<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>

<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

</Project>
uselessxp
uselessxpOP2y ago
oh, it seems the -windows suffix was the problem
uselessxp
uselessxpOP2y ago
this seems to have solved the problem
ero
ero2y ago
nope, it also needs to be WinExe
uselessxp
uselessxpOP2y ago
and I can also remove the using
ero
ero2y ago
sure doesn't matter i like to remove them to be specific about what i globally use
uselessxp
uselessxpOP2y ago
interesting, the app run with those settings, but crash on clipboard.settext the app crashes if I put WinExe and/or net7.0 target framework
uselessxp
uselessxpOP2y ago
I get this on startup with WinExe and net7.0-windows target framework, he pretend the input string not null, maybe it's just a 7.0 news and I have to declare and inizialize it?
uselessxp
uselessxpOP2y ago
ok not working also if I assign them a value
uselessxp
uselessxpOP2y ago
btw if I comment all for try only to set clipboard I get another error Clipboard OLE calls causes System.Threading.ThreadStateException it seems harder than I thought
uselessxp
uselessxpOP2y ago
I skip for now, I think those are not concepts for a beginner
Want results from more Discord servers?
Add your server