/// <summary>
/// Adds a given resourcename and data to the app resources in the target assembly
/// </summary>
/// <param name="ResourceName"></param>
/// <param name="ResourceData"></param>
/// <remarks></remarks>
public void Add(string ResourceName, byte[] ResourceData)
{
// make sure the writer is initialized
InitAssembly();
// have to enumerate this way
for (var x = 0; x <= _Resources.Count - 1; x++)
{
var res = _Resources[x];
if (res.Name.Contains(".Resources.resources"))
{
// Have to assume this is the root application's .net resources.
// That might not be the case though.
// cast as embeded resource to get at the data
var EmbededResource = (Mono.Cecil.EmbeddedResource)res;
// a Resource reader is required to read the resource data
var ResReader = new ResourceReader(new MemoryStream(EmbededResource.GetResourceData()));
// Use this output stream to capture all the resource data from the
// existing resource block, so we can add the new resource into it
var MemStreamOut = new MemoryStream();
var ResWriter = new System.Resources.ResourceWriter(MemStreamOut);
var ResEnumerator = ResReader.GetEnumerator();
byte[] resdata = null;
while (ResEnumerator.MoveNext())
{
var resname = (string)ResEnumerator.Key;
string restype = "";
// if we come across a resource named the same as the one
// we're about to add, skip it
if (Strings.StrComp(resname, ResourceName, CompareMethod.Text) != 0)
{
ResReader.GetResourceData(resname, out restype, out resdata);
ResWriter.AddResourceData(resname, restype, resdata);
}
}
// add the new resource data here
ResWriter.AddResourceData(ResourceName, "ResourceTypeCode.ByteArray", ResourceData);
// gotta call this to render the memory stream
ResWriter.Generate();
// update the resource
var buf = MemStreamOut.ToArray();
var NewEmbedRes = new EmbeddedResource(res.Name, res.Attributes, buf);
_Resources.Remove(res);
_Resources.Add(NewEmbedRes);
// gotta bail out, there can't be 2 embedded resource chunks, right?
break; // TODO: might not be correct. Was : Exit For
}
}
}