Skip to content
GrahamTheCoder edited this page Dec 11, 2021 · 28 revisions

Before performing any conversion, it's recommended to:

  • Ensure you have no uncommitted changes in your version control system. This will make it easy to see the result of the conversion, and discard any parts you don't want.
  • Create a new branch
  • Ensure your solution compiles for maximum conversion accuracy.
  • Split out any files over about 5000 lines (using partial classes if needed). Converting two 5000 line files is usually significantly faster than converting one 10000 line file. This may be slightly less of an issue with the command line.
  • Immediately after conversion, commit the result, this will make it easy to report issues
    • I'd always recommend separating automated from manual steps in commits initially - it makes it so much easier to make use of the history to locate/correct mistakes, after all, what else is the history for?
    • Converting a project to a totally different language is inherently somewhat messy and has a whole bunch of steps as you've just described. Trying to pretend it all happened in one go will just make life harder 3 months down the line when you notice an issue and can't figure out at which point in the process it got introduced.
    • If being on a branch isn't enough, and for whatever reasons you need to sweep the mess under the rug later, in git it's pretty straightforward using rebase (there's even an option in GitHub to squash rebase a branch).

Once your output is compiling, I'd recommend setting up a .editorconfig and using: dotnet-format to format things the way you like.

Using the Visual Studio extension

  • Install the Visual Studio Extension in VS2019+ (even free community edition is fine)
  • Open your solution in Visual Studio

Converting whole project/solution

  • Right click the solution or project node to convert in the solution explorer and select "Convert to VB" or "Convert to C#".
  • The Output window will show progress - it may seem to pause for several minutes at times on large projects.
  • At the end of the process you'll be given the choice to automatically update any references (from non-converted projects and the solution) to reference the now converted projects. Cancelling the process at this point will leave all original files intact, and converted projects/files on disk to be manually inspected.
  • When the conversion finishes, the Output window will show a summary of the conversion's success, and one of the converted files will be opened as an example.
  • Once you're happy with the resulting conversion, you may want to delete all files with (for example) ".vb" in their name recursively from a folder:
    • In powershell: Get-ChildItem -Recurse *.vb* | Remove-Item ⚠️ Deletes files recursively!
    • Windows command line: DEL /S /Q *.vb* ⚠️ Deletes files recursively!

Converting a single file

  • Right click the .vb or .cs file to convert in the solution explorer and select "Convert to VB" or "Convert to C#".
  • The Output window will show progress - it may seem to pause for several minutes since related projects must be compiled.
  • When the conversion finishes, the Output window will show a summary of the conversion's success.
  • The converted file will be saved to disk adjacent to the original file (but with the appropriate converted file extension), and opened in a code window. It won't be part of any project or solution.

Converting a snippet

  • Open a .vb or .cs file containing code to be converted.
  • Highlight the relevant text.
  • Right click on the highlighted text and select "Convert to VB" or "Convert to C#".
  • The Output window will show progress - it may seem to pause for several minutes since related projects must be compiled.
  • When the conversion finishes, the Output window will show a summary of the conversion's success.
  • The converted snippet will be saved to disk adjacent to the original file (but with the appropriate converted file extension), and opened in a code window. It won't be part of any project or solution.

Selected text conversion context menu

Alternatively you can use the online snippet converter: https://codeconverter.icsharpcode.net/ However, it is less accurate due to the lack of project context.

Cancelling

Closing Visual Studio, or starting a new conversion cancels the in-progress conversion

Using the command line

codeconv --help

Usage: codeconv [options] <Source solution path>

Arguments:
  Source solution path                         The solution containing project(s) to be converted.

Options:
  -h|--help                                    Show help information
  -i|--include                                 Regex matching project file paths to convert. Can be used multiple times
  -e|--exclude                                 Regex matching project file paths to exclude from conversion. Can be used
                                               multiple times
  -t|--target-language <CS | VB>               The language to convert to.
  -f|--force                                   Wipe the output directory before conversion
  --core-only                                  Force dot net core build if converting only .NET Core projects and seeing
                                               pre-conversion compile errors
  -b|--best-effort                             Overrides warnings about compilation issues with input, and attempts a
                                               best effort conversion anyway
  -o|--output-directory                        Empty or non-existent directory to copy the solution directory to, then
                                               write the output.
  -p|--build-property <Configuration=Release>  Set build properties in format: propertyName=propertyValue. Can be used
                                               multiple times
  • Converts all projects in a solution from VB.NET to C#.
  • Please backup / commit your files to source control before use.
  • We recommend running the conversion in-place (i.e. not specifying an output directory) for best performance.
  • See https://github.com/icsharpcode/CodeConverter for the source code, issues, Visual Studio extension and other info.
Clone this wiki locally