public override IDictionaryEnumerator GetEnumerator() { ResXResourceSet resx = resxResourceSet; ResourceSet compiled = compiledResourceSet; if (resx == null || compiled == null) { Throw.ObjectDisposedException(); } // changing is checked in resx resource set return(new Enumerator(this, (ResXResourceEnumerator)resx.GetEnumerator(), compiled.GetEnumerator(), ((IResXResourceContainer)resx).Version)); }
public void SetRemoveObject() { var path = Combine(Files.GetExecutingPath(), "Resources", "TestResourceResX.resx"); var rs = new ResXResourceSet(path, null); // replace Assert.IsTrue(rs.GetObject("TestString") is string); rs.SetObject("TestString", 1); Assert.AreEqual(1, rs.GetObject("TestString")); // add new Assert.IsNull(rs.GetObject("NotExist")); rs.SetObject("NotExist", 2); Assert.IsNotNull(rs.GetObject("NotExist")); // delete rs.RemoveObject("TestString"); Assert.IsNull(rs.GetObject("TestString")); Assert.IsFalse(rs.GetEnumerator().GetKeysEnumerator().Any(e => e == "TestString")); // nullifying rs.SetObject("NotExist", null); Assert.IsNull(rs.GetObject("TestString")); Assert.IsTrue(rs.GetEnumerator().GetKeysEnumerator().Any(e => e == "NotExist")); // save and reload StringBuilder sb = new StringBuilder(); #if NETCOREAPP2_0 || NETCOREAPP3_0 RemoveUnsupportedItems(rs); #endif rs.Save(new StringWriter(sb)); var rsReloaded = new ResXResourceSet(new StringReader(sb.ToString()), Path.GetDirectoryName(path)); AssertItemsEqual(rs, rsReloaded); }
static IEnumerable <Tuple <string, string> > GetValues(string resxFilePath) { var values = new List <Tuple <string, string> >(); using (var resx = new ResXResourceSet(resxFilePath)) { var en = resx.GetEnumerator(); while (en.MoveNext()) { var name = (string)en.Key; var value = resx .GetString(name) .Replace(Environment.NewLine, " ") .Replace("&", "-"); values.Add(new Tuple <string, string>(name, value)); } } return(values.OrderBy(x => x.Item1).ToList()); }
public static void Main() { CreateResXFile(); ResXResourceSet resSet = new ResXResourceSet(@".\StoreResources.resx"); IDictionaryEnumerator dict = resSet.GetEnumerator(); while (dict.MoveNext()) { string key = (string)dict.Key; // Retrieve resource by name. if (dict.Value is string) { Console.WriteLine("{0}: {1}", key, resSet.GetString(key)); } else { Console.WriteLine("{0}: {1}", key, resSet.GetObject(key)); } } }
private void RemoveUnsupportedItems(ResXResourceSet rs) { string[] unsupported = #if NETCOREAPP2_0 // .NET Core 2.0 Drawing and WinForms types are not supported { "System.Drawing", "System.Windows.Forms" }; #else // .NET Core 3.0 and above: Drawing and WinForms types are supported, only binary serialized types are removed new string[0]; #endif bool origMode = rs.SafeMode; rs.SafeMode = true; foreach (var item in rs.GetEnumerator().ToEnumerable <string, ResXDataNode>().ToList()) { if (item.Value.AssemblyQualifiedName?.ContainsAny(unsupported) == true || item.Value.FileRef?.TypeName.ContainsAny(unsupported) == true || item.Value.MimeType == ResXCommon.BinSerializedObjectMimeType) { rs.RemoveObject(item.Key); } } rs.SafeMode = origMode; }
private static Dictionary <string, string> GetResourceKeyValueList(string resourceFilePath) { Dictionary <string, string> resourceKeyValues = new Dictionary <string, string>(); ResXResourceSet resSet = new ResXResourceSet(resourceFilePath); IDictionaryEnumerator dict = resSet.GetEnumerator(); while (dict.MoveNext()) { string key = (string)dict.Key; if (dict.Value is string) { resourceKeyValues.Add(key, resSet.GetString(key)); } else { resourceKeyValues.Add(key, resSet.GetObject(key).ToString()); } } return(resourceKeyValues); }