开发者

How to delete resource entry from resx in C#?

I know how to retrieve and create resource entries in an .resx file programmatically. I'm using the ResourceWriter and the ResourceReader for that approach. But now I want to clean up several items in my .res开发者_如何学Pythonx file, and want to delete them. How can I achieve that?


Using the ResXResourceReader and ResXResourceWriter classes.

Read your resx using the reader and feed it into the writer, omitting the entries you want removed.


In my work I had to develop to remove from a culture file what I had in a standard culture file.

The code is this:

private void ResolveConflicts()
        {
            using (var cultura = new ResXResourceReader(CulturaPath))
            using (var culturaCustom = new ResXResourceReader(CulturaCustomPath))
            {
                Resources = (from cc in culturaCustom.Cast<DictionaryEntry>()
                             where !(from c in cultura.Cast<DictionaryEntry>()
                                     select new { c.Key, c.Value })
                             .Contains(new { cc.Key, cc.Value })
                             select new Tuple<string, string>(
                                  cc.Key.ToString(),
                                  cc.Value.ToString())).ToList();
            }

            ReWriteCustomFile();
        }

        private void ReWriteCustomFile()
        {
            using (var writer = new ResXResourceWriter(CulturaCustomPath))
            {
                Resources.ForEach(r => writer.AddResource(r.Item1, r.Item2));

                writer.Generate();
                writer.Close();
            }
        }

With this code, it is recorded in the custom culture only what is different from the default.

I hope it helps, thanks!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜