(使用vb.net) 嗨, 我有一个ini文件,我需要在ini文件中将RTF文件作为单行发布 – 比如… [my section]rtf_file_1=bla bla bla (the content of the rtf file) 为了避免RTF文件中的换行符,特殊代码等包装在ini文
嗨,
我有一个ini文件,我需要在ini文件中将RTF文件作为单行发布 – 比如…
[my section] rtf_file_1=bla bla bla (the content of the rtf file)
为了避免RTF文件中的换行符,特殊代码等包装在ini文件中,如何将其编码(并解码)为单个字符串?
如果有一个函数将字符串(在我的情况下是RTF文件的内容)转换为一行数字然后解码回来,我就是这个东西?
你会怎么做?
谢谢!
您可以使用base64编码对它们进行编码.就像那样,内容被处理为二进制 – >它可以是任何类型的文件.但是当然在配置文件中你将无法读取文件的内容.
这里是一个Snodpet到Base64的编码/解码
//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));
}
在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
