How to replace string in resource .NET assembly?

1,309 views
Skip to first unread message

NIkNik

unread,
Mar 6, 2014, 3:29:27 AM3/6/14
to mono-...@googlegroups.com
Hello.

I need find and replace string in dll.


foreach (Resource res in assembly.MainModule.Resources) { if(res.ResourceType != ResourceType.Embedded) { continue; } byte[] x = ((EmbeddedResource)res).GetResourceData(); ??? }

Please, help. Thanks.

Jb Evain

unread,
Mar 6, 2014, 3:34:40 AM3/6/14
to mono-...@googlegroups.com
Hi,

It all depends on the format of embedded data. Is this a simple text
file? Is this a windows resource file?

Jb
> --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mono-cecil+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

NIkNik

unread,
Mar 6, 2014, 3:50:48 AM3/6/14
to mono-...@googlegroups.com

It all depends on the format of embedded data. Is this a simple text
file? Is this a windows resource file?

Text in xml tags.
>>The .resx resource file format consists of XML entries, which specify objects and strings inside XML tags.

Jb Evain

unread,
Mar 6, 2014, 3:58:05 AM3/6/14
to mono-...@googlegroups.com
You're going to have to know the encoding of the data.

Then you can either simply transform the data to a string using the
proper encoding, or create a memory stream for the data and pass that
to your favorite XML api.

Jb

NIkNik

unread,
Mar 8, 2014, 3:56:00 PM3/8/14
to mono-...@googlegroups.com
Hello!

if i open dll in text editor, i see System.Resources.RuntimeResourceSet.
And than list dll.
How get this list, and change public token dll.

Jb Evain

unread,
Mar 10, 2014, 9:03:06 AM3/10/14
to mono-...@googlegroups.com
Hi,

This is a bit outside of the scope of Cecil itself. At that point in
time you only need to find a way to read your resource set from
memory, and write it back to memory.

That being said, here's some code that you could use:

var resource = (EmbeddedResource) module.Resources.Single (r => r.Name
== "Foo.resources");

var reader = new ResourceReader (resource.GetResourceStream ());
foreach (DictionaryEntry kv in reader)
Console.WriteLine("Key: {0} Value: {1}", kv.Key, kv.Value);

var newResourceStream = new MemoryStream ();
var writer = new ResourceWriter (newResourceStream);

foreach (DictionaryEntry kv in reader) {
if (kv.Value is string)
writer.AddResource((string)kv.Key, (string)kv.Value);
else if (kv.Value is byte[])
writer.AddResource ((string) kv.Key, (byte[]) kv.Value);
else
writer.AddResource ((string) kv.Key, kv.Value);
}

var newResource = new EmbeddedResource (resource.Name,
resource.Attributes, newResourceStream.ToArray ());
module.Resources.Remove (resource);
module.Resources.Add (newResource);

There's probably a much simpler way.
> --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mono-cecil+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Badr Yasuo

unread,
Jul 25, 2019, 12:21:07 PM7/25/19
to mono-cecil
THANK YOU! <3
On Sat, Mar 8, 2014 at 9:56 PM, NIkNik <nikolay...@gmail.com> wrote:
> Hello!
>
> if i open dll in text editor, i see System.Resources.RuntimeResourceSet.
> And than list dll.
> How get this list, and change public token dll.
>
> --
> --
> --
> mono-cecil
> ---
> You received this message because you are subscribed to the Google Groups
> "mono-cecil" group.
> To unsubscribe from this group and stop receiving emails from it, send an
Reply all
Reply to author
Forward
0 new messages