Compile a .NET2.0 DLL from .NET4.0 - c#

I am using .NET4.0, but for compatability reasons, I'd like to compile to a .NET2.0 dll from c#. There should be no .NET4.0 specific functionality used in the script, so it should run fine in a .NET2.0 environment. Is there some commandline syntax on the csc that I can specify a version number with?

You mentioned csc.exe, so I'm assuming that you won't be building with Visual Studio, but rather, by command line. Also, I'm assuming that msbuild is not available on the build machine.
I believe that csc.exe is specific to each version. For example, in the folder C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319, you will find csc.exe, and in the folder C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727, you will find another version of csc.exe.
To build a .NET 2.0 dll, you should reference the csc.exe from the v2.0 folder (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727).
Cheers!

In Visual Studio you could set the target framework version to .NET 2.0 in the properties of the project:

if you are compiling manually from the command line, can't you just run the v2 framework csc?
eg (paths from my machine)
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe
or for v4
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe

If you build with MSBuild (which of course includes from within VS) then you can specify the target framework version from the project properties dialog. However, if you build manually it seems there is no surefire way to express that restriction.

Set the target framework to 2.0 in the project's properties. In case you are using features like LINQ that are not present on the 2.0 framework, this approach won't work. If you need full compatibility with 2.0 framework, you should write your code for the 2.0 and then compile targeting the 4.0 later if you need.

Related

In Linux , The reference assemblies for framework ".NETFramework,Version=v4.5" were not found

I have made a setup of Visual studio to compile C# code in my Ubuntu Machine .
I loaded the workspace/my code to VS and I could see the below error.
The reference assemblies for framework ".NETFramework,Version=v4.5" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.
Please help me on resolving this issue as am a beginner in Visual studio.
Thanks
If project is heavy, you can follow
this procedure
If your project is lightweight, create a new .netcore project using VS and move your code (and dependencies references)into that new project. then let VS telling you potential errors and correct them.
Looking at the procedure, you can firstly retargeting your actual project in dotnet 4.6.2 framework in order to "ensures that you can use API alternatives for .NET Framework-specific targets in the cases where .NET Core can't support a particular API."
I would recommend running the portability tool in Visual Studio 2017 if you have it. This will give you an idea if you will have a hard time moving it over.
As for your error with the csproj, that's because that file has paths within it, which are pointing to locations using windows paths, instead of Linux paths.

Visual Studio 2017 - Potentially unneeded dlls in release folder

i'm trying to obtain an executable file with only the necessary dlls in the release folder, unfortunately VS is putting in that folder even System dlls, that afaik are not supposed to be redistributed with the executable.
I'd like to know if there's a way to make VS add to the release folder only dlls the executable can not work without, the ones that are supposed to be redistributed.
Thank you.
Release folder:
These assemblies are shims for .Net Standard where the full framework had gaps. Based on the assemblies in that folder, I assume that your exe is targeting .net 4.6.x and you are consuming a .net standard library.
If you upgrade to .net 4.7.2 you will see the number of required System.* assemblies reduced greatly.
See this answer for more details: Why does my .NET Standard NuGet package trigger so many dependencies?
I'm assuming this is a dotnet core project. Use the "dotnet publish" command to create your deployment files (the screenshot looks like a VS build directory?). You might see more dlls than you're used too if you're coming to dotnet core from using the Windows frameworks previously, but it depends on the publish options you use.
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish

C# Setup with external DLL

I am trying to make a setup for a C# application in .NET 3.5.
The application runs an other application which was compiled for .NET 4.5 and uses some DLLs. I want to add this application to the application folder in the setup.
I can't add the DLL's to the setup as a file, there is an error popping up. ("The operation could not be completed")
If I add the DDLs to the project folder and then use them as content, I get an "Unrecoverable build error" when I create the project.
How am I supposed to add these DLLs? I do not care how, but I need them in the project folder.
Thanks.
PS: I am using the standard setup for VisualStudio 2008.
With Visual Studio, when you add your external DLL as a reference in a project, it will automatically be added to the setup.
First of all, isn't there any way to find an earlier version of the assembly targeting .NET 3.5? Or if you have access to the source, remove/change the .NET 4.5 specific code and recompile?
Otherwise, you can try to wrap you dll around a COM interface, as described here. This article targets .NET 4 dll used with .NET 2.0 but the mechanism should still work in your configuration. I have used it successfully myself for 2.0/4.0 interop.
Here is another trick you can try.
Consider that in either case you will need .NET 4.5 installed on target computer, in order to work. So you can move your project from 3.5 to 4.5. I understand that you use VS2008 which doesn't have .NET 4.5, but you can use express (free) edition of Visual Studio from here -> Microsoft Download Page
I solved this problem using a simple trick:
The errors came only from the DLLs written in .NET 4.5. The executable (.exe) did not cause any errors. What I did is to package all the DLL's into the exe using the ILMerge tool.
Then I added the .exe file to the library and everything worked like a charm.
ILMerge download site (Microsoft)

Which version csc.exe should I use?

I am following a tutorial to consume WSDL in a C# aplication:
http://my.execpc.com/~gopalan/dotnet/webservices/webservice_csharp_client.html
At step 2, it says do following:
C:\MyProjects\Cornucopia\WebService\Client>csc /t:library /r:System.Web.Services.dll /r:System.Xml.dll OIDServer.cs
But csc.exe was not in my path so ı have searched for it and the result:
There are 20 different csc.exe in my computer. Which one should I use?
I am running Windows 8 x64 EE.
Usually you want to use the one in C:\WINDOWS\Microsoft.NET\Framework\vx.x.xxxxx\.
Where the x.x.xxxxx is the Framework version you are targeting. So if I were trying to use the C# 4.0 compiler, I would use "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"
Just press Start then start typing "Developer" and select Developer Command Prompt for VS2012.
As vcjones said use the one that is in the .NET Framework directory with the the lowest .NET Framework version you want to support.
The other csc.exe are only backups of the .NET Framework stored in the WinSxs folder which are installed by the MSI installer. Since the .NET Framework ist part of the OS there is always a backup in the WinSXS folder present to be able to do a restore when something bad did happen. But since the .NET Framework did use already a different location
%Windir%\Microsoft.NET...
Now we have two locations where the .NET Framework files are located. The other versions you did see were different patch levels of the .NET Framework which do stay in WinSxs for "backwards" compat reasons which does not makes sense for .NET Framework parts but for unmanaged dlls.

Compile VS2010 c# solution (project) on ubuntu

What's the best way to build a c# solution made with Visual Studio on Ubuntu? Is there a way to convert the .sln file to a makefile? Should I use Mono?
The first thing I would do would be to use Moma to check to see if your program will run under Mono as is. You can also use MonoDevelop which can use Visual Studio Projects.
From their faq: In fact, since MonoDevelop 2.0 the default project format has been VS2008-style MSBuild projects, but VS2005 and VS2010 formats are also handled.
C# is a .NET language, .NET is a Windows-based framework. It has been ported to Linux operating systems (Ubuntu included) via the Mono Project. So yes, you need to use Mono.
Mono is the best I've tried. It says on their homepage that they are binary compatible between each other, so if it's already built, you could just run it on Mono. No need to recompile.
Your best bet is to use Mono if you want to use a process like MsBuild. Mono has xBuild that is similar. This SO question has some information about using Mono.

Categories

Resources