【问题标题】:How can we add embedded resources to a file which is compiled from a source file at run-time我们如何将嵌入式资源添加到在运行时从源文件编译的文件中
【发布时间】:2012-04-19 18:13:06
【问题描述】:

我正在编写一个小型应用程序,它使用函数从源 (.cs) 代码文件编译文件:

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;
}

我想要实现的是添加用户选择的文件资源(图像、mp3、avi、ttf、..etc),这些资源将作为嵌入式资源添加到使用上述函数编译的对象中。

我们如何将嵌入式资源添加到在运行时从源文件编译的文件中?

【问题讨论】:

    标签: c# .net embedded-resource csharpcodeprovider


    【解决方案1】:

    您需要create a .resources file 包含要作为资源嵌入的文件,然后使用EmbeddedResources 属性在CompilerParameter 的实例中引用生成的资源文件。

    按照上面第一个链接的.resources Files 中的资源 部分(引用System.Resources.ResourceWriter 的部分)中的说明进行操作,这将生成一个临时文件。然后根据您问题中的代码(以及EmbeddedResources 文档中的示例),您将使用以下内容引用它:

    if (provider.Supports(GeneratorSupport.Resources))
    {
        cp.EmbeddedResources.Add("pathToYourGeneratedResourceFile");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 2013-03-06
      相关资源
      最近更新 更多