写入resx文件c#
Writing to resx file c#
正在尝试写入以 xml 编写的 Resx 文件。
我将如何添加列和行。
List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"], ConfigurationManager.AppSettings["FrenPath"], ConfigurationManager.AppSettings["RusPath"] };
ResourceWriter resourceWriter = new ResourceWriter(_paths.ElementAt(0));
resourceWriter.AddResource("Key1", "String1");
resourceWriter.AddResource("Key2", "String2");
resourceWriter.Close();
我想添加 key1 并在该行旁边的列中包含 string1 等等。
我想我不明白 msdn 是如何解释资源编写器的使用方式的。
您错过了 resourceWriter.Generate()
电话。
List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"], ConfigurationManager.AppSettings["FrenPath"], ConfigurationManager.AppSettings["RusPath"] };
using(ResXResourceWriter resourceWriter = new ResXResourceWriter(_paths.ElementAt(0)))
{
resourceWriter.AddResource("Key1", "String1");
resourceWriter.AddResource("Key2", "String2");
resourceWriter.Generate();
}
编辑。如果您丢失了旧密钥,您可以将它们存储在散列 table 中,将新密钥添加到散列 table 并从散列 table.
重新生成 resx
using System.Resources;
List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"], ConfigurationManager.AppSettings["FrenPath"], ConfigurationManager.AppSettings["RusPath"] };
Hashtable oHt = new Hashtable();
// Read the keys and store in a hash table
using (ResXResourceReader oReader = new ResXResourceReader(_paths.ElementAt(0)))
{
IDictionaryEnumerator oResource = oReader.GetEnumerator();
while (oResource.MoveNext())
oHt.Add(oResource.Key,oResource.Value);
}
//Add the new keys to the hash table
oHt["Key1"] = "String1";
oHt["Key2"] = "String2";
//Re-generate the new resx from the hash table
using (ResXResourceWriter oWriter = new ResXResourceWriter(_paths.ElementAt(0)))
{
foreach (string key in oHt.Keys)
oWriter.AddResource(key.ToString(), oHt[key].ToString());
oWriter.Generate();
}
你调用的方法确实是正确的,也是MSDN建议的方法。
您传递给 ResourceWriter
的构造函数的参数是您希望存储资源文件的路径,包括文件名。
可以使用相对(仅文件名 "myStrings.resources"
)或绝对(完整文件路径 "C:\Users\Folder\myStrings.resources"
)。 稍后会详细介绍 \
...
我不知道您的第一个 _paths
元素是什么值,但请确保它的格式正确,适合您要存储文件的位置。 请告诉我您的 _paths.AtElement(0)
字符串是什么;我或许可以进一步提供帮助。
请注意 如果您使用的是绝对文件路径,请确保您的 \
在日常 "C:\Users\User1\AFolder" 位置(来自 Windows文件浏览器)被转义;这是通过将 \
放在前面来完成的。
因此,例如文件夹 C:\Users\User1\AFolder
实际上应该转义并写入 "C:\Users\User1\AFolder"
.
这样的字符串
同样重要 确保使用 using
作为 IResourceWriter
class 实现 IDisposable
.
通过将您的代码包装在 using 语句中来做到这一点:
using (ResourceWriter resourceWriter = new ResourceWriter(_paths.ElementAt(0))
{
resourceWriter.AddResource("Key1", "String1");
resourceWriter.AddResource("Key2", "String2");
resourceWriter.Close();
}
在使用 IDisposable
导数的地方使用 using
是一个很好的做法。
希望对您有所帮助!
知道了!
必须添加来自解决方案资源管理器的引用。
添加对 System.Windows.Forms.
的引用
然后将 System.Resources 添加到 class 文件的顶部。
现在可以使用 ResXResourceWriter! :)
正在尝试写入以 xml 编写的 Resx 文件。
我将如何添加列和行。
List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"], ConfigurationManager.AppSettings["FrenPath"], ConfigurationManager.AppSettings["RusPath"] };
ResourceWriter resourceWriter = new ResourceWriter(_paths.ElementAt(0));
resourceWriter.AddResource("Key1", "String1");
resourceWriter.AddResource("Key2", "String2");
resourceWriter.Close();
我想添加 key1 并在该行旁边的列中包含 string1 等等。
我想我不明白 msdn 是如何解释资源编写器的使用方式的。
您错过了 resourceWriter.Generate()
电话。
List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"], ConfigurationManager.AppSettings["FrenPath"], ConfigurationManager.AppSettings["RusPath"] };
using(ResXResourceWriter resourceWriter = new ResXResourceWriter(_paths.ElementAt(0)))
{
resourceWriter.AddResource("Key1", "String1");
resourceWriter.AddResource("Key2", "String2");
resourceWriter.Generate();
}
编辑。如果您丢失了旧密钥,您可以将它们存储在散列 table 中,将新密钥添加到散列 table 并从散列 table.
重新生成 resxusing System.Resources;
List<string> _paths = new List<string> { ConfigurationManager.AppSettings["SpanPath"], ConfigurationManager.AppSettings["FrenPath"], ConfigurationManager.AppSettings["RusPath"] };
Hashtable oHt = new Hashtable();
// Read the keys and store in a hash table
using (ResXResourceReader oReader = new ResXResourceReader(_paths.ElementAt(0)))
{
IDictionaryEnumerator oResource = oReader.GetEnumerator();
while (oResource.MoveNext())
oHt.Add(oResource.Key,oResource.Value);
}
//Add the new keys to the hash table
oHt["Key1"] = "String1";
oHt["Key2"] = "String2";
//Re-generate the new resx from the hash table
using (ResXResourceWriter oWriter = new ResXResourceWriter(_paths.ElementAt(0)))
{
foreach (string key in oHt.Keys)
oWriter.AddResource(key.ToString(), oHt[key].ToString());
oWriter.Generate();
}
你调用的方法确实是正确的,也是MSDN建议的方法。
您传递给 ResourceWriter
的构造函数的参数是您希望存储资源文件的路径,包括文件名。
可以使用相对(仅文件名 "myStrings.resources"
)或绝对(完整文件路径 "C:\Users\Folder\myStrings.resources"
)。 稍后会详细介绍 \
...
我不知道您的第一个 _paths
元素是什么值,但请确保它的格式正确,适合您要存储文件的位置。 请告诉我您的 _paths.AtElement(0)
字符串是什么;我或许可以进一步提供帮助。
请注意 如果您使用的是绝对文件路径,请确保您的 \
在日常 "C:\Users\User1\AFolder" 位置(来自 Windows文件浏览器)被转义;这是通过将 \
放在前面来完成的。
因此,例如文件夹 C:\Users\User1\AFolder
实际上应该转义并写入 "C:\Users\User1\AFolder"
.
同样重要 确保使用 using
作为 IResourceWriter
class 实现 IDisposable
.
通过将您的代码包装在 using 语句中来做到这一点:
using (ResourceWriter resourceWriter = new ResourceWriter(_paths.ElementAt(0))
{
resourceWriter.AddResource("Key1", "String1");
resourceWriter.AddResource("Key2", "String2");
resourceWriter.Close();
}
在使用 IDisposable
导数的地方使用 using
是一个很好的做法。
希望对您有所帮助!
知道了!
必须添加来自解决方案资源管理器的引用。 添加对 System.Windows.Forms.
的引用然后将 System.Resources 添加到 class 文件的顶部。
现在可以使用 ResXResourceWriter! :)