【发布时间】:2016-09-28 03:56:41
【问题描述】:
我目前正在学习如何将 C# 代码编译成他们自己的 exe 文件。 它的工作原理真是令人着迷!
到目前为止,我已经有了这个非常小的应用程序,它应该将放入文本框中的文本转换为 C# 代码,这有点像,但它一直告诉我每次按“构建”时都要添加引用,我不知道为什么。
这里是源代码:
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace SimpleBuilder
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// store code
/// select what features
/// print out textfile with all the code from the features
/// compile that textfileContent to a exe
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void fakeMessageOne()
{
Messagebox.Show("Hello World");
}
private void button_Click(object sender, RoutedEventArgs e)
{
CSharpCodeProvider csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", frameworkTextbox.Text } });
CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, outputTextbox.Text, true);
parameters.GenerateExecutable = true;
CompilerResults result = csc.CompileAssemblyFromSource(parameters, sourceTextbox.Text);
if (result.Errors.HasErrors)
{
result.Errors.Cast<CompilerError>().ToList().ForEach(error => errorTextbox.Text += error.ErrorText + "\r\n");
}
else
{
errorTextbox.Text = "--- Build Succeeded! ---";
}
}
}
}
这是它给我带来的错误
The type or namespace name 'CSharp' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
The type or namespace name 'CodeDom' does not exist in the namespace 'System' (are you missing an assembly reference?)
The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?)
The type or namespace name 'Window' could not be found (are you missing a using directive or an assembly reference?)
我试图在我的应用程序中编译的代码:
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
namespace SimpleBuilder
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// store code
/// select what features
/// print out textfile with all the code from the features
/// compile that textfileContent to a exe
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void fakeMessageOne()
{
Messagebox.Show("Hello World");
}
}
}
【问题讨论】:
-
您能告诉我们您要编译的代码吗?
-
对不起!我现在就加!
-
您是否使用 Visual Studio 来构建它?
-
是的,好吧,要构建应用程序,然后我想构建源代码,但应用程序给了我错误,添加了一张图片。
-
您需要添加对编译器的引用。不是你的项目。
CSharpCodeProvider不会自动链接到您的项目链接到的任何程序集。
标签: c# .net wpf winforms compilation