简体   繁体   English

从源文件编译程序时,如何将目标框架设置为v2.0或v3.5而不是4.0?

[英]How to set the target framework to v2.0 or v3.5 instead of 4.0 when compiling a program from a source file?

I have a small application which compiles a source file into executable. 我有一个小型应用程序,它将源文件编译为可执行文件。 The problem is that this application require Netframework 4 to work, and therfore, the compiled one also require the Net Framework 4.0 问题在于此应用程序需要Netframework 4才能工作,因此,已编译的应用程序也需要Net Framework 4.0

How can i set to the function below the target framework to be used in the compiled application ? 如何设置要在已编译应用程序中使用的目标框架下的功能?

  public static bool CompileExecutable(String sourceName)
{
//Source file that you are compliling 
FileInfo sourceFile = new FileInfo(sourceName);
//Create a C# code provider 
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
//Create a bool variable for to to use after the complie proccess to see if there are any erros
bool compileOk = false;
 //Make a name for the exe
 String exeName = String.Format(@"{0}\{1}.exe",
 System.Environment.CurrentDirectory, sourceFile.Name.Replace(".", "_"));
 //Creates a variable, cp, to set the complier parameters
 CompilerParameters cp = new CompilerParameters();
 //You can generate a dll or a exe file, in this case we'll make an exe so we set this to true
 cp.GenerateExecutable = true;
 //Set the name
 cp.OutputAssembly = exeName;
 //Save the exe as a physical file
 cp.GenerateInMemory = false;
 //Set the compliere to not treat warranings as erros
 cp.TreatWarningsAsErrors = false;
 //Make it compile 
 CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceName);
 //if there are more then 0 erros...
 if (cr.Errors.Count > 0)
 {
     //A message box shows the erros that occured 
     MessageBox.Show("Errors building {0} into {1}" +
         sourceName + cr.PathToAssembly);
     //for each error that occured in the code make a separete message box
     foreach (CompilerError ce in cr.Errors)
     {
         MessageBox.Show("  {0}" + ce.ToString());
     }
 }
 //if there are no erros...
 else
 {
     //a message box shows compliere results and a success message
     MessageBox.Show("Source {0} built into {1} successfully." +
         sourceName + cr.PathToAssembly);
 }
 //if there are erros...
 if (cr.Errors.Count > 0)
 {
     //the bool variable that we made in the beggining is set to flase so the functions returns a false
     compileOk = false;
 }
 //if there are no erros...
 else
 {
     //we are returning a true (success)
     compileOk = true;
 }
 //return the result
 return compileOk;
}

Any help would be appreciated ! 任何帮助,将不胜感激 ! Thankyou in advance 先感谢您

If your using a CodeDomProvider to compile code yourself programmatically in VS 2008, what version of the Framework is targeted? 如果您使用CodeDomProvider在VS 2008中以编程方式自己编译代码,那么目标是哪个版本的Framework?

By default it is 2.0, irrespective of which VS 2010 target version (2.0, 3.0 or 3.5, 4.0) is specified. 默认情况下,它是2.0,无论指定哪个VS 2010目标版本(2.0、3.0或3.5、4.0)。

In order to target the 4.0 framework, provide an IDictionary instance in the provider's constructor as shown below 为了以4.0框架为目标,请在提供程序的构造函数中提供一个IDictionary实例,如下所示

You could pass options to the compiler using the following constructor: 您可以使用以下构造函数将选项传递给编译器:

var providerOptions = new Dictionary<string, string>();
providerOptions.Add("CompilerVersion", "v4.0");
var compiler = new CSharpCodeProvider(providerOptions);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将目标框架从v4.0更改为v3.5后,找不到命名空间 - Namespace cannot be found after changing target framework from v4.0 to v3.5 .NET Framework从v3.5迁移到v4.0 - NET Framework migration from v3.5 to v4.0 调试时,托管旧版(v3.5,v3.0,v2.0)和托管旧版(v4.5,v4.0)的默认设置在哪里? - Where is the default setting for Managed Legacy (v3.5, v3.0, v2.0) vs Managed Legacy (v4.5, v4.0) when debugging? 如何在Windows 8中处理.Net Framework版本V4.0和V3.5 - How to deal with .Net framework versions V4.0 vs V3.5 in Windows 8 如何使用.Net框架v3.5调用异步任务 - How to call async Task using the .Net framework v3.5 使用.netFramework版本4.6.1而不是项目目标框架.NETStandard v2.0还原了EF6.2 - EF6.2 was restored using .netFramework version 4.6.1 instead of the project target framework .NETStandard v2.0 如何从NopCommerce v3.5的设置表中加载记录 - How to load records from Setting table of NopCommerce v3.5 使用&#39;.NETFramework,Version = v4.6.1&#39;而不是项目目标框架&#39;.NETCoreApp,Version = v2.0&#39;恢复了包&#39;EntityFramework 6.2.0&#39; - Package 'EntityFramework 6.2.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0' 使用Kinect for Windows从SDK v2.0加载来自文件的视频 - Load video from file with Kinect for Windows with SDK v2.0 即使安装了CF v3.5,也会缺少MissingMethodException吗? - MissingMethodException even when CF v3.5 is installed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM