C
C#15mo ago
Surihia

❔ How do I use the System.Windows.Forms namespace in a .net core compiled console app ?

I want to use the open file dialog box and the messagebox functionalities from this namespace in my console app. the former is for selecting a path of a particular file by which my app does few file related operations and the messagebox is used for displaying success and error messageboxes.
23 Replies
JakenVeina
JakenVeina15mo ago
you don't if you want to use Windows GUI controls out of System.Windows.Forms, you're not making a console app, you're making a Windows Forms app
Surihia
Surihia15mo ago
then is there any way by which I can spawn the open file dialog box to set the paths ? or more appropriately is there a way by which I can let an user set the path of a particular file via the console ?
JakenVeina
JakenVeina15mo ago
I mean, yeah, it's the console you can give the user a prompt to put in a file path if you want to use Windows components, like the dialog, you're not making a Console app, like I said which you can do
Surihia
Surihia15mo ago
hmm...I also have the option of going back to 4.8 framework for this as console apps made with that do support the winforms namespace but as it is ancient, I don't want to use that.
JakenVeina
JakenVeina15mo ago
they don't they're not console apps
Surihia
Surihia15mo ago
so 4.8 framework compiled console apps aren't console apps ?
JakenVeina
JakenVeina15mo ago
if they use Windows components, no
Surihia
Surihia15mo ago
This is a snippet from one of my console apps. it does use winforms components. https://github.com/Surihix/FFXIIIMovieAudioMod/blob/master/Core.cs
JakenVeina
JakenVeina15mo ago
that makes it not a Console app
Surihia
Surihia15mo ago
oh ok. thanks for helping out with this.
JakenVeina
JakenVeina15mo ago
if you really want the dialogs, just look into making a WinForms or WPF app in NET 5+ if you still want a console on top of that, you can do that too
Ice_
Ice_15mo ago
It should still be possible to open a dialog window even from console. Maybe not by built-in stuff but I can imagine it's plausible through WinApi?
Surihia
Surihia15mo ago
I have an approach which can theoretically use the winforms as well as the console too but its a bit hacky in nature
Ice_
Ice_15mo ago
It's not supported though so yeah, it's surely hacky
Surihia
Surihia15mo ago
my method is to basically have the open dialog thingy be a winforms app and lauch the app via the console this would mean that I have to use multiple apps for this probably two of them
Ice_
Ice_15mo ago
And then return the file name from your forms app to the application which launched it?
Surihia
Surihia15mo ago
yup. like that.
Ice_
Ice_15mo ago
I like the idea. I know it's crazy and I know it's not the way of doing things but I like the idea of thinking outside the box. That's where all the brilliancy comes from
Surihia
Surihia15mo ago
last option would be to use framework and make everything a single app. but framework is outdated and I don't want to use that.
Ice_
Ice_15mo ago
I remember in the old days where wanted persistent data so I just appended it to the currently running exe file at the end, and then had a function to read that data so people could store data and read it by just passing the executable. Not a very fancy way of doing things but...hacky.
Surihia
Surihia15mo ago
yeah sometimes we have to do such approaches according to the situation. I still consider myself new to C# and have a lot to learn
Ice_
Ice_15mo ago
Are you new to programming in general? Oh nevermind, you told above C sharp is a great language 🙂
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.