2016-09-28 65 views
2

我目前正在學習如何將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"); 
     } 
    } 
} 

而且爲什麼它的價值 here is a image of the small application

+0

你能告訴我們你正在編譯的代碼嗎? – Rob

+0

對不起! Il現在就添加它! –

+0

您是否使用Visual Studio構建它? –

回答

2

你應該把你的參考文獻與CompilerParameters.ReferencedAssemblies.Add()

+0

我試過'parameters.ReferencedAssemblies.Add(「Microsoft.CSharp」);'無法找到',但現在它扔我這 '元數據文件'Microsoft.CSharp'找不到' –

+0

儘管[文檔](https://msdn.microsoft.com/en-us/library/system。 codedom.compiler.compilerparameters.referencedassemblies(v = vs.110).aspx),爲程序集的元數據文件添加完整或部分路徑,而不是程序集的_name_。大多數程序集由一個文件組成,因此它將是裝配元數據的文件。無論如何,.exe或.dll文件(如果這樣的文件是程序集的一部分)幾乎總是包含元數據的文件。因此,添加'parameters.ReferencedAssemblies.Add(「Microsoft.CSharp」);' –

+0

哎呀。我的意思是在最後加上「.dll」:'parameters.ReferencedAssemblies.Add(「Microsoft.CSharp.dll」);' –

相關問題