2015-01-12 48 views
0

我想使用C#應用程序來創建一個單獨的.exe,並且似乎我已經出現錯誤。CodeDom編譯錯誤(非法字符在路徑)

使用下面的代碼,我得到這個錯誤:

System.ArgumentException: Illegal characters in path. 
    at System.IO.Path.GetFileName(String path) 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) 
    at System.IO.File.OpenRead(String path) 
    at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames) 
    at Program_Name.Core.BuildStub(String FileName, String mutex) in c:\Users\Tom\Documents\Visual Studio 2013\Projects\Program_Name\Program_Name\Core.cs:line 45 
    at Program_Name.Builder.button1_Click(Object sender, EventArgs e) in c:\Users\Tom\Documents\Visual Studio 2013\Projects\Program_Name\Program_Name\Builder.cs:line 30 

,這是什麼原因呢?我使用的代碼關閉msdn.microsoft.com(鏈接:http://msdn.microsoft.com/en-us/library/saf5ce06(v=vs.110).aspx

我的代碼如下:

public static void BuildStub(string FileName, string mutex) 
{ 
    CSharpCodeProvider provider = new CSharpCodeProvider(); 
    CompilerParameters cp = new CompilerParameters(); 

    cp.ReferencedAssemblies.Add("System.dll"); 

    cp.GenerateExecutable = true; 

    cp.OutputAssembly = "name.exe"; 

    cp.GenerateInMemory = false; 
    string sourceFile = Properties.Resources.SourceCode; 

    CompilerResults cr = provider.CompileAssemblyFromFile(cp, sourceFile); 

    if (cr.Errors.Count > 0) 
    { 
     Console.WriteLine("Errors building {0} into {1}", 
       sourceFile, cr.PathToAssembly); 
     foreach (CompilerError ce in cr.Errors) 
     { 
      Console.WriteLine("{0}", ce.ToString()); 
      Console.WriteLine(); 
     } 
    } 
    else 
    { 
     Console.WriteLine("Source {0} built into {1} successfully.", 
      sourceFile, cr.PathToAssembly); 
    } 

    // Return the results of compilation. 
    if (cr.Errors.Count > 0) 
     Console.WriteLine("There were errors RIP"); 
} 

我試圖建立(Properties.Resources.SourceCode)的代碼是:

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

     } 
    } 
} 

回答