Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
210 views
in Technique[技术] by (71.8m points)

c# - How can we add embedded resources to a file which is compiled from a source file at run-time

I'm writing a small application which works at compiling a file from a source (.cs) code file using a function:

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

What i want to achieve is to add user-selected file resources ( images, mp3, avi, ttf,..etc ) which will be added as embedded resources to the being compiled using the function above.

how can we add embedded resources to a file which is compiled from a source file at run-time ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You'll need to create a .resources file containing the files you want to embed as resources, and then reference the generated resource file in your CompilerParameter's instance using the EmbeddedResources property.

Follow the directions from Resources in .resources Files section of the first link above (the section that refers to System.Resources.ResourceWriter), which will produce a temporary file. Then based on the code in your question (and the example from the EmbeddedResources documentation), you'll reference it with something like:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...