Question

I am trying to build a program which will compile one .cs with an xml file as a resource. I have the following code for compiling:

private void Builder(bool run)
{
    CodeDomProvider compiler = CodeDomProvider.CreateProvider("CSharp");
    string output = "Output.exe";
    string inputXml = "Input.xml";
    string xml = File.ReadAllText(inputXml);
    string inputCS = Properties.Resources.Program;
    IResourceWriter writer = new ResourceWriter("temp.resources");
    writer.AddResource("Story", xml);
    writer.Close();
    CompilerParameters parameters = new CompilerParameters();
    //Make sure we generate an EXE, not a DLL
    parameters.GenerateExecutable = true;
    parameters.OutputAssembly = output;
    parameters.GenerateInMemory = false;
    parameters.ReferencedAssemblies.Add("System.dll");
    parameters.ReferencedAssemblies.Add("mscorlib.dll");
    parameters.ReferencedAssemblies.Add("System.Xml.dll");
    parameters.EmbeddedResources.Add("temp.resources");
    CompilerResults results = compiler.CompileAssemblyFromSource(parameters, inputCS);
    if (run)
    {
        Process.Start(output);
    }
}

But it has compilation errors and says Properties is not a valid reference from Properties.Resources.Story:

Code from cs file to be compiled

static bool LoadData()
{
    bool result = true;
    Program.doc = new XmlDocument();
    doc.LoadXml(Properties.Resources.Story);
    return result;
}

Update: This is the specific error:

c:\Users\Nick\AppData\Local\Temp\zk14fqrm.0.cs(38,25) : Error CS0103: The name 'Properties' does not exist in the current context

So what about this needs to change to fix this?

Was it helpful?

Solution 2

Yes, that's to be expected. The Properties.Resources class is automatically generated by a visual studio code generator. It's not a feature of the C# compiler.

So you still need to add the respective Resources.Designer.cs file, or you need to invoke the code generator as part of your building process and add the autogenerated file to the list of cs files to compile.

OTHER TIPS

    writer.AddResource("Story", xml);

That does not do what you hope it does. There is a lot more to embedding resources than meets the eye. Lots of work done by the IDE and msbuild.exe to make the Properties.Resources code work at runtime. The rough steps:

  • Project + Properties, Resource tab, Add Resource generates an .xml file. Presumably one that's very similar to the one you are using in your code.
  • That also auto-generates code, you can see it by opening the Properties + Resources.resx + Resources.Designer.cs file. It is this file that declares the Properties.Resources class. This step is not being done in your code which explains the compile error
  • At build time, the .resx file is translated from XML to a format that's more compact and suitable to be embedded inside an assembly. This is done by the Resgen.exe build tool. It translates from the .xml file to a .resources file. Note how you got that wrong as well, you are writing the xml to the .resources file instead of the Resgen.exe output.

Reproducing all this with CodeDom does get to be quite painful, particularly the auto-generated code step. Practical approaches are keeping the resources in files instead of trying to embed them, embedding a single resource and retrieving at runtime with Assembly.GetManifestResourceStream() and using ResourceManager explicitly as shown in the Resources.Designer.cs file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top