简体   繁体   中英

Writing to resx file c#

Trying to write to a Resx file written in xml.

How would i go about adding columns, and rows to it.

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();

I would like to add key1 and have the column beside it on that row to have string1 and so on.

I think I am not understanding how the msdn has explained the way the resource writer should be used.

You are missing the resourceWriter.Generate() call.

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();
}

Edit . If you are losing the old keys you can store them in a hash table,add the new keys to the hash table and regenerate the resx from the hash table.

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();
}

The methods you are calling are indeed correct and the way that MSDN has advised.

The parameter you pass into the constructor for ResourceWriter is the path at where you would like the resource file to be stored including the filename.

Either relative (just the file name "myStrings.resources" ) or absolute (full file path "C:\\\\Users\\\\Folder\\\\myStrings.resources" ) can be used. More on the \\\\ in a moment...

I don't know what value your first _paths element is, but be sure that it is properly formed for where you would like the file to be stored. If you could let me know what your _paths.AtElement(0) string is; I could perhaps help further.

PLEASE NOTE If you are using an absolute file path, ensure that your \\ in your everyday "C:\\Users\\User1\\AFolder" location (from Windows file browser) are escaped; this is done by placing \\ in front.

So, for example a folder C:\\Users\\User1\\AFolder should actually be escaped and written in a string like "C:\\\\Users\\\\User1\\\\AFolder" .

EQUALLY AS IMPORTANT Make sure you use using as the IResourceWriter class implements IDisposable .

Do this by wrapping your code in a using statement:

using (ResourceWriter resourceWriter = new ResourceWriter(_paths.ElementAt(0))
{
    resourceWriter.AddResource("Key1", "String1");
    resourceWriter.AddResource("Key2", "String2");
    resourceWriter.Close();
}

This is good practice to use using where an IDisposable derivative is used.

Hope this helps!

Got it!

Had to add reference from the solution explorer. Add reference to System.Windows.Forms.

Then add System.Resources to top of class file.

Works now with ResXResourceWriter! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM