(using vb.net)
Hi,
I have an ini file where I need to post an RTF file as a single line in the ini file - like...
[my section]
rtf_file_1=bla bla bla (the content of the rtf file)
To avoid linebreaks, special codes etc. in the RTF file from wrapping in the ini file, how can I encode (and decode) it as a single string?
I was thing if there was a function to convert a string (in my case the content of the RTF file) into a line of numbers and then decode it back?
What would you do?
Thanks!
You can encode them using base64 encoding. Like that the content gets treated binary --> it can be any type of file. But of course in the config file you wont be able to read the content of the file.
Here a Snippet to Base64 encode / decode
//Encode
string filePath = "";
string base64encoded = null;
using (StreamReader r = new StreamReader(File.OpenRead(filePath)))
{
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(r.ReadToEnd());
base64encoded = System.Convert.ToBase64String(data);
}
//decode --> write back
using(StreamWriter w = new StreamWriter(File.Create(filePath)))
{
byte[] data = System.Convert.FromBase64String(base64encoded);
w.Write(System.Text.ASCIIEncoding.ASCII.GetString(data));
}
and in VB.NET:
Dim filePath As String = ""
Dim base64encoded As String = vbNull
'Encode()
Using r As StreamReader = New StreamReader(File.OpenRead(filePath))
Dim data As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(r.ReadToEnd())
base64encoded = System.Convert.ToBase64String(data)
End Using
'decode --> write back
Using w As StreamWriter = New StreamWriter(File.Create(filePath))
Dim data As Byte() = System.Convert.FromBase64String(base64encoded)
w.Write(System.Text.ASCIIEncoding.ASCII.GetString(data))
End Using
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With