Targeting Mono in Visual Studio 2012
These steps are known good on my Windows 8 machine, with Visual Studio 2012 w/ Update 1 and Mono 2.10.9.
- Install Mono for Windows, from http://www.go-mono.com/mono-downloads/download.html
Choose a decent path, which for me wasC:\Program Files (x86)\Mono-2.10.9
- Load an elevated administrative Command Prompt (Top tip: On Windows 8, hit WinKey+X then choose “Command Prompt (Admin)“)
- From this Command Prompt, execute the following commands (in order):
$ cd "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile"
$ mklink /d Mono "C:\Program Files (x86)\Mono-2.10.9\lib\mono\4.0"
$ cd Mono
$ mkdir RedistList
$ cd RedistList
$ notepad FrameworkList.xml
- Notepad will start and ask about creating a new file, choose Yes.
- Now paste in this text and Save the file:
<?xml version="1.0" encoding="UTF-8"?>
<FileList ToolsVersion="4.0" RuntimeVersion="4.0" Name="Mono 2.10.9 Profile" Redist="Mono_2.10.9">
</FileList>
- From the same Command Prompt, type:
$ regedit
- In the Registry Editor, navigate to:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319\SKUs\
and create a new Key folder called.NETFramework,Version=v4.0,Profile=Mono
- Now navigate to
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319\SKUs\
and create the same Key folder again here (this step is only necessary for x64 machines, but since all software developers use those then you’ll probably need to do it!) - Now load up VS2012 and a project. Goto the Project Properties (Alt+Enter whilst having it selected on Solution Explorer) .
- Choose the Application tab on the left and look for the “Target framework” drop-down list.
- On this list you should see an entry called “Mono 2.10.9 Profile”.
- Select it, and Visual Studio should then convert your project to target the Mono framework. You’ll notice that it will re-reference all your various System assemblies and if you study the filenames they will point to the Mono ones that were created during Step #3.
Note: I was scratching my head at first as I kept getting an error from Visual Studio saying:
This application requires one of the following versions of the .NET Framework:
.NETFramework,Version=v4.0,Profile=MonoDo you want to install this .NET Framework version now?
Turns out that even on a x64 machine you MUST add both Registry key SKUs (see Steps #7 and #8). It is not enough to just add the Wow6432Node key, you must add the other as well. I can only assume this is because VS2012 is a 32-bit application. But maybe it’s also affected by whether you’re compiling to Any CPU, x64 or x86… who knows. It doesn’t really matter as long as this fixes it, which it does!
Building and executing your first program
Now that your development environment is nicely setup, you can proceed and build your first program.
The Mono equivalent of MSBuild is called XBuild (not sure why they didn’t call it MBuild or something!). You can build your .csproj by doing the following:
- Load the Mono Command Prompt (it will be on your Start Menu/Screen, just search for “mono”).
- Change directory to your project folder.
- Execute the following command to build your project using the Mono compiler:
$ xbuild /p:TargetFrameworkProfile=""
Note: You must specify the blankTargetFrameworkProfile
parameter as otherwise the compiler will issue warnings along the lines of:
Unable to find framework corresponding to the target framework moniker ‘.NETFramework,Version=v4.0,Profile=Mono’. Framework assembly references will be resolved from the GAC, which might not be the intended behavior.
- Hopefully you’ll not have any errors from the build…
- Now you can run your program using the Mono runtime, to do this:
$ mono --gc=sgen "bin\Debug\helloworld.exe"
Note: You'll definitely want to use the "Sgen" garbage collector (hence the parameter) as the default one in Mono is unbearably slow. - You can do a quick “smoke test” to verify everything is in order with both your compilation and execution. Have your program execute something like:
Console.Write(typeof (Console).Assembly.CodeBase);
… and this should print out a path similar to:
file:///C:/PROGRA~2/MONO-2~1.9/lib/mono/4.0/mscorlib.dll
I’ve no idea why it prints it out using 8.3 filename format, but there you go! You’ll notice that if you run your program outside of the Mono runtime then it will pick up the Microsoft CLR version from the GAC.
Hi.
Point 11. does not work for me (VS2012 v.11.05 Update 1, 64bit pc) . “On this list you should see an entry called “Mono 2.10.9 Profile”. I can’t
Ok I found. RedistList directory shoud be inside Mono directory.
In point 3 shoud be added (after mklink line) :
$ cd “C:\Program Files (x86)\Mono-2.10.9\lib\mono\4.0”
thanks good tutorial.
Thanks, not sure how that got missed. Have updated the article.
Hi Nathan,
I was trying to use Mono profiles from my Visual Studio 2010 installation and this article points me in the right direction; I was missing the registry key entries.
In my case, I wanted to maintain differents mono profile versions working inside VS2010, so I made a few modifications that may be useful to others to know.
In point 3, I create a directory with the name “Mono_2.10.9” (instead of a Mono directory/link). That difference must be taken into account when creating the registry keys, so, my registry keys for my installation is: “.NETFramework,Version=v4.0,Profile=Mono_2.10.9” instead of “.NETFramework,Version=v4.0,Profile=Mono”
Also in point 5, the xml file should be different across the varios versions for “Name” and “Redist” attributes of “FileList” node.
After I use Xbuild, it gives me 52 errors and 0 warnings saying that “Not Valid As An Identifier”. I’m using Visual Basic 2010 Ultimate. Also when I change my project to the Mono Profile, I get 11 errors in Visual Studio. Like “ShutDownStyle” is not a member of XXX.
What to here : [ xbuild /p:TargetFrameworkProfile=”” ] ?!
I mean what’s the Target frame work ?
I tried “Mono” and other several things but all didn’t work.
And thank you for this guide.
I want to set up .NET 3.5 with Mono. What can I do?
Nathan,
My pc is 32bit with WinVista and Visual Sudio is 2005. I followed point 8 up to HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft and do not see .NETFramework. Now is there anything equivalent for 32bit machines to what you suggested?
Thanks,
Homeyra
Tested & worked. Thanks a lot !
Updates for v4.5 .NET:
http://www.reddit.com/r/csharp/comments/1u9bku/any_way_to_target_mono_in_vs2012/ceg7qvc
Great Article. But Works only with Windows Application. Does not show the Mono in the target frame work when trying to change for ASP.NET applications.
Also does not work (Windows or Web) for 3.5. Worked for 4.0 only.
I followed your steps (but for Mono version 4.0.1). After building the application I ended up with the identical output as I did when I compiled for Target .Net 4.0
Very confusing – I am trying to port a legacy C# on Windows project to Mac. Installing the Mono framework went fine. It builds without problems on VS 2012 targeted to mono-4.0.1 and I can run it in the command line box with mono. On Xamarin I have problems. I can build it with one warning when I target at .net run-time and use msbuild. If I target it at mono 4.0.1 run-time I get one error. If I use xbuild, I get 7 errors even if I build using xbuild directly in the command line box. All errors relate to resource files of graphical objects not being found. Is mono buggy? is Xamarin buggy?
“xbuild is not recognised as an internal or external command, operable program or batch file” error on the Mono command prompt. I tried changing env variable Path to the C>Windows>Microsoft.NET>Framework>v4.030319;
Does not help. any idea why?
install mono, install monohelper extension, do the configurations as mentioned, run it with xbuild in monohelper. everything gonna be fine. I’ve tested it on win8.1
Dose it work with Mono framework 4.6.1(.net Framework 4.6.1)
[…] Targeting Mono in Visual Studio 2012 […]