Convert Winform App to Console app

2019-02-22 06:38发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

In Visual Studio 2015, under the Project menu select Properties then under Output Type change the drop down from Windows Application to Console Application. Build Solution. Done.