2016-12-13 27 views
2

如何給我的roslyn編譯器輸出一個圖標?用Roslyn發送Win32Icon

在CodeDOM的,我可以簡單地使用CompilerOptions並將其添加爲「/ win32icon:」

但如何在羅斯林辦?

我已經讚了。

var syntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText("program.cs")); 
     CSharpCommandLineArguments arguments = new CSharpCommandLineArguments(); 
     arguments.Win32Icon = @"ICON PATH"; 

     CSharpCompilation compilation = CSharpCompilation.Create(
      "program", 
      new[] { syntaxTree }, 
      new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) }, 
      new CSharpCompilationOptions(OutputKind.ConsoleApplication)); 

     ResourceWriter rs = new ResourceWriter("res.resources"); 

     var resourceDescription = new ResourceDescription("program.Resources.resources",() => new MemoryStream(File.ReadAllBytes("res.resources")), true); 

     using (var dllStream = new MemoryStream()) 
     using (var pdbStream = new MemoryStream()) 
     { 
      var emitResult = compilation.Emit(dllStream, pdbStream, null,null, manifestResources: new [] {resourceDescription}); 
      if (emitResult.Success) 
      { 
       File.WriteAllBytes("test.exe",dllStream.GetBuffer()); 
       Console.WriteLine("compiled"); 
      } 
      else 
      { 
       Console.WriteLine("foutje: {0}", emitResult.Diagnostics[0].ToString()); 
      } 
     } 

     Console.ReadLine(); 

感謝您的幫助!

回答

1

您需要將win32Resources選項傳遞給Emit,通過Compilation.CreateDefaultWin32Resources創建,該參數需要參數Stream iconInIcoFormat

compilation.Emit(
    peStream: peStream, 
    pdbStream: pdbStream, 
    win32Resources: compilation.CreateDefaultWin32Resources(..., iconInIcoFormat: File.Open("<pathTo.ico>"))) 

的更詳細的〔實施例是here