Vb.net 将ANSI加密文件中的文本加载到RichTextBox中

Vb.net 将ANSI加密文件中的文本加载到RichTextBox中,vb.net,winforms,Vb.net,Winforms,我是一名初级程序员,目前正在寻找一种方法,以我的形式将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.Def

我是一名初级程序员,目前正在寻找一种方法,以我的形式将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编码而不是加密

您可以与列表中的一个代码页一起使用,而不是使用
编码。默认值
。下面是我上面链接的
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

要查看所需的代码页是否可用,可以使用获取它们的列表。

ANSI编码可能意味着任何编码

Encoding.Default
将返回Windows中的默认代码页(它取决于Windows中运行程序的区域设置)

如果文件使用其他代码页编码,则必须将StreamReader编码设置为该代码页,如下所示:

Encoding.GetEncoding(1250);
1250是中欧和东欧拉丁语代码页


稍后添加:

您询问过如何以ANSI格式保存—只需使用与
StreamReader
编码相同的
StreamWriter


一些代码页示例和更多关于维基百科代码页的信息:

我将添加我正在尝试的代码。请阅读维基百科上关于加密和(字符)编码的内容。这些在英语中是非常不同的东西。+1对于
GetEncodings
,我不知道这个方法,但是我担心你的代码会吓到初学者。@Kamil:代码示例来自链接的MSDN文章;这不是我的。:-)如果MS认为这是一个很好的样本,我该和谁争论呢?:-)我想我一生中写的VB/VB.NET代码不会超过一两行。我编辑了我的答案,以确保代码显然来自该链接目标,而不是由我编写的。谢谢。:-)