✅ 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
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
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
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?
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)how could I know myselft that a COM reference can't be flagged and imported normally, without this additional step?
i don't even know what in the world a COM reference is
never used it ever
the same, I suppose is when you add a not external reference, but "an already existing one"
looks like COM is legacy
don't use it
oh, well^^
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.
of course there's other ways
.net7 windows forms apps exist
I mean to do it on a ConsoleApp project
oh, it seems the
-windows
suffix was the problemthis seems to have solved the problem
nope, it also needs to be
WinExe
and I can also remove the using
sure
doesn't matter
i like to remove them to be specific about what i globally use
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
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?ok not working also if I assign them a value
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
I skip for now, I think those are not concepts for a beginner