views:

59

answers:

3

Is there a quick and dirty way (or a VS Macro) to convert a WinForms app to a Console App? I have a number of these apps that are no longer needed as Winforms apps. I suppose I could create a bunch of new projects and copy paste modules and classes over, but if it's just a matter of removing the single form that I have and editing/deleting a few things, I'd rather do that.

+3  A: 

No, there is not. It's extremely dependent upon how the program was originally written. If the original programmer made the system fairly modular, this shouldn't take you too long. If all of the logic is in the WinForms code-behind, you have some serious work to do.

It's manual programming/refactoring work.

Dave Markle
This is the most realistic, informative, and practical answer given of the 3 so far.
Heath Hunnicutt
Thanks Dave for the insight.
Otaku
It is not realistic if you have to change 50+ applications like I did.
AMissico
+8  A: 

You can change the project settings to create a Console Application instead of a Windows Application (just hit Ctrl+Enter on the project in the Solution Window, and make that change).

That will compile the application as a Console Application, and you'll get a console. However, you'll still get your forms. If you want to not have Windows Forms, you'll need to remove the forms, and replace their functionality with command line arguments or configuration. You can then remove the Windows Forms references, and it'll be converted.

Reed Copsey
This is a terrible answer. It leads the user to the "click a VS button does the trick" line of thinking which got him into this mess. The question of whether he can convert his code is *entirely* a matter of rewriting the I/O to not use forms. This is the critical piece of work, not merely changing the output EXE file header, as you suggest.
Heath Hunnicutt
@Heath: That was why I said, specifically, that he would need to replace the form functionality with command line arguments or configuration. I was never suggesting that this is an automatic process - but rather these are the steps required to convert form forms to console, which I feel is 100% accurate.
Reed Copsey
@Reed. Your answer makes it seem like changing the EXE file is a big chunk of the work. Actually, it is a trivial setting that accomplishes very little. Then you have one English language clause indicating that he will have to "replace their functionality." Yeah, and that will be 99.9999% of the effort. Meanwhile, following your advice and not AMissico's better advice, he has trashed his existing VS project...
Heath Hunnicutt
@Reed. In answer to Otaku's question, "Is there a button which does this for me," the answer is "No, and you need to stop thinking that way." Your answer is essentially, "Oh, yes, VS takes care of that for you, and leaves you only a minor amount of work remaining..." which is ridiculous.
Heath Hunnicutt
@Heath: You can disagree with me, but I haven't edited this, because I still feel that this is the most complete answer here. AMissico's advice is to hand edit the VS project - that's going to be much more likely to trash the project file in a way that's completely uneditable in VS than using VS's tools to make the changes. You don't have to agree with me, though, since SO's not a popularity contest ;)
Reed Copsey
@Heath: I never said anything about it being minor. If the form is doing nothing but showing a message, then yes, this is minor. If the forms are doing a lot of work, it's not. It really depends on the project in question.
Reed Copsey
@Heath: I'd also suggest re-reading @Otaku's question in full. He never asked for a "single button" to do this - and his wording makes it clear that he realizes he'll need to remove the form and edit some of his code.
Reed Copsey
@Reed, thank you for permission to disagree with you. Without your permission, I would be at a complete loss as to how I should feel.
Heath Hunnicutt
@Reed, he wrote "quick and dirty way". So, by implication, the project setting you have pointed out to him is a "quick and dirty" step on the way to being done. But it is a dumb step. AMissico is suggesting hand-edit of the resulting VS project, which will not harm the existing project. So, yes, his answer, while not the best, is superior to yours.
Heath Hunnicutt
+1, this is the correct answer of course. The OP already knows he needs to replace the Application.Run call.
Hans Passant
@Reed Copsey: This is just what I needed. My WinForms footprint are really low (just one form as mentioned above), so this will probably be the easiest.
Otaku
+2  A: 

When I had to do this, I create a blank WinForm and blank Console project then compared the differences. The differences are minor. I opened all the projects as XML files in Visual Studio then did a search-and-replace as needed based on the differences. Worked well.

I then opened each project and fixed any errors, such as missing entry-point method, and so on.

Also, you can record a temporary-macro to do some of the mundane stuff.


Update Based on Comments

The project file differences between a "Windows Forms" and "Console" application are minor. Maybe I should have used the work trival.

All you need to do is change <OutputType>WinExe</OutputType> to <OutputType>Exe</OutputType>. If a programmer cannot make simple search-and-replace with open files without making a mistake, they shouldn't be programming.

I had to do this with 50+ applications for a client. One consultant told them it would take a week to make the changes. I told my client that I could do it in an hour and on-site. The hard part of this job was getting stuck in traffic.

I opened all project files as XML then changed the OutputType and StartupObject in the project files using search-and-replace "in open documents".

I created a macro that would add a "program" VB.NET Module and another for a C# Class with the needed "main" entry point and application-run code.

Once at this point, I wrote a small program that would load each project in Visual Studio 2008. I ran the appropriate macro depending if the project was VB.NET or C#. I would fix any errors, compile, run the application, then close. The small program with then start another Visual Studio with the next project. The hardest part of the utility was getting the paths to all the projects because I had to use DOS, as in dir /b /s *.vbproj > c:\project_listing.txt.

If the OP only has a handful of projects then I suggest they open each project and make the needed changes.

I have used a similar technique to convert about 20 WinCE/WinForms&Console/VB.NET projects to Desktop/WinForms&Console/VB.NET projects where I had to convert project and form files that contained major differences, especially the form files.

In my opinion, you people need to lighten up. None of this matters ten minutes from now.

AMissico
Thanks AMissico.
Otaku