[英]Load text from ANSI encrypted file into a RichTextBox
我是一名初学者程序员,目前正在寻找一种将ANSI加密文件中的文本加载到表单中的RichTextBox
。 另外,我正在寻找一种使用ANSI加密将其保存回文件的方法。
这是我目前正在尝试的方法:
Imports System
Imports System.IO
Imports System.Text
Dim strResult As String
Using SR As StreamReader = New StreamReader("startup_config.cfg", Encoding.Default)
strResult = SR.ReadToEnd
End Using
我相信您的意思是ANSI 编码而不是加密 。
您可以将Encoding.GetEncoding
与此处列表中的代码页之一一起使用,而不是使用Encoding.Default
。 这是我上面链接的GetEncoding
页面中的代码示例:
Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Namespace Convert_Example
Class MyConvertExampleClass
Shared Sub Main()
Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H03A0) & ")"
' Create two different encodings.
Dim ascii As Encoding = Encoding.ASCII
Dim [unicode] As Encoding = Encoding.Unicode
' Convert the string into a byte[].
Dim unicodeBytes As Byte() = [unicode].GetBytes(unicodeString)
' Perform the conversion from one encoding to the other.
Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)
' Convert the new byte[] into a char[] and then into a string.
' This is a slightly different approach to converting to illustrate
' the use of GetCharCount/GetChars.
Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)) As Char
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
Dim asciiString As New String(asciiChars)
' Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString)
Console.WriteLine("Ascii converted string: {0}", asciiString)
End Sub
End Class
End Namespace
要查看所需的代码页是否可用,可以使用GetEncodings
获取它们的列表。
ANSI编码可以表示任何编码。
Encoding.Default
将在Windows中返回默认的代码页(取决于运行程序的Windows中的区域设置)。
如果文件使用其他代码页编码,则必须将StreamReader编码设置为该代码页,如下所示:
Encoding.GetEncoding(1250);
1250是中东欧拉丁语代码页。
稍后添加:
您询问是否要以ANSI格式进行保存-只需使用StreamWriter
,其编码与StreamReader
相同。
很少有代码页示例,以及有关Wikipedia上代码页的更多信息: 链接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.