Click here to Skip to main content
15,442,578 members
Articles / Programming Languages / C#
Posted 20 Nov 2007


44 bookmarked

Code Convert Visual Studio Add-in for C# and VB.NET

Rate me:
Please Sign up or sign in to vote.
4.24/5 (11 votes)
20 Nov 2007CPOL1 min read
Adds a convert from C# to VB.NET and from VB.NET to C# button to the tools menu


This solution contains an add-in project for Visual Studio.

The add-in adds a button to the tools menu for converting VB.NET code from and to C#.

The solution also contains a set up project for the add-in.


I created this project as part of my efforts to study add-in programming. I chose code conversion because I couldn't find one that enables conversion from C# to VB.NET and from VB.NET to C# easily enough.

The add-in uses an external service which can be found here.

Screenshot - ctovb.jpg

You can easily change the provider or use local code (if you have it - I couldn't find a good example for a local converter and that is why I used the external service).

Note: You must be connected to the Internet in order to use the add-in while it's using the external service.

Using the Code

This is the Exec method which runs whenever the user hits the button:

public void Exec(string commandName, vsCommandExecOption executeOption, 
		ref object varIn, ref object varOut, ref bool handled)
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
                if (commandName == "CodeConvert.Connect.CodeConvert")
                    handled = true;
                    string url = 
                    string parameters = String.Empty;
                    string remark = String.Empty;
                    string code = Clipboard.GetData(DataFormats.Text).ToString();
                    if (_applicationObject.ActiveDocument.Name.EndsWith("cs"))
                        remark = "//C# converted code, conversion by: 
                " + 
                        parameters = "Code=" + System.Web.HttpUtility.UrlEncode(code) + 
                    else if (_applicationObject.ActiveDocument.Name.EndsWith("vb"))
                        remark = "'VB.NET converted code, conversion by: 
			  " + 
                        parameters = "Code=" + System.Web.HttpUtility.UrlEncode(code) + 
                    if (remark != String.Empty)
                        string result = HttpWebRequestHttpPost(url, parameters);
                               .Text = remark + result + Environment.NewLine; ;
			("CodeConvert can be used for .vb or .cs files only");
        catch (Exception ex)
            MessageBox.Show("Error:" + ex.Message);

The add-in knows what to convert to by the current file extension - so when you want to convert VB.NET to C#, you need to be in a *.cs file pasting VB.NET code and when trying to convert C# to VB.NET, you need to be in a *.vb file pasting C# code (pasting -> hitting the button with the original code in your clipboard).


Screenshot - vb.jpg

Figure 1: Original VB.NET Code (Copy).

Screenshot - csbefore.jpg

Figure 2: Tools Menu.

Screenshot - csafter.jpg

Figure 3: C# code is pasted in the target file.



  • 20th November, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Israel Israel
Working for the last 10 years as a developer / team leader.

Getting more and more into automated unit tests & architecture design.

Now days working with various Microsoft products - BizTalk, Share Point, MCMS and C# in general.

Comments and Discussions

QuestionCan you release The Translate.ashx file Pin
mcprichy1-Jun-20 18:23
Membermcprichy1-Jun-20 18:23 
AnswerHey Check out this add-in Pin
vCillusion4-Sep-14 19:04
MembervCillusion4-Sep-14 19:04 
Generalsir can you Convert code to Pin
Roome9998-Sep-09 16:00
MemberRoome9998-Sep-09 16:00 
QuestionHow do I install? Pin
gthune12-May-09 17:28
Membergthune12-May-09 17:28 
QuestionHow do I install it? Pin
RoarkDude22-Jun-08 21:28
MemberRoarkDude22-Jun-08 21:28 
AnswerRe: How do I install it? Pin
loid grey manuel25-Oct-10 19:58
Memberloid grey manuel25-Oct-10 19:58 
Generala good local source for convertor between c# to vb Pin
pita200020-Nov-07 7:26
Memberpita200020-Nov-07 7:26 
GeneralRe: a good local source for convertor between c# to vb Pin
Doron Goldberg20-Nov-07 8:31
MemberDoron Goldberg20-Nov-07 8:31 
GeneralCool! Pin
rippo20-Nov-07 5:30
Memberrippo20-Nov-07 5:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.