简体   繁体   中英

Renaming elements within a .properties file using C#

I am trying to rename an element inside a application.properties file using C#. This is currently being accomplished via a batch file but it would be good if I could directly acheive this with C#.

For example inside my application.properties file I have the value 'name'. With a batch file which is being run from within C# I can update the element 'name' in this file on the fly.

Im assuming there is a way to accomplish this in C# without the need for a batch file.

Managed to solve this using BinaryWriter.

using (BinaryWriter bw = new BinaryWriter(File.Open(ApppropFile, FileMode.Open)))
        {
            //write new data
            string strNewData = "NEW DATA";
            byte[] byteNewData = new byte[strNewData.Length];

            // copy contents of string to byte array
            for (var i = 0; i < strNewData.Length; i++)
            {
                byteNewData[i] = Convert.ToByte(strNewData[i]);
            }

            // write new data to file
            bw.Seek(141, SeekOrigin.Begin);  // seek to position 


            bw.Write(byteNewData, 0, byteNewData.Length);
        }

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