2013-05-25 2 views
-1

저는 초보 프로그래머로 현재 ANSI 암호화 파일의 텍스트를 RichTextBox에로드하는 방법을 찾고 있습니다. 또한 ANSI 암호화를 사용하여 파일에 저장하는 방법을 찾고 있습니다.ANSI 암호화 된 파일의 텍스트를 RichTextBox로로드하십시오.

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 
+0

내가 암호화와 (문자) 인코딩에 대한 위키 백과에 읽어 보시기 바랍니다 – user1877499

+0

을 시도하고 코드를 추가합니다. 이들은 영어로 매우 다른 것들입니다. – Kamil

답변

4

난 당신이 ANSI 인코딩 대신 암호화를 의미 믿습니다

이 내가 현재 시도하고 나는 것입니다.

Encoding.GetEncodingEncoding.Default 대신 here 목록의 코드 페이지 중 하나와 함께 사용할 수 있습니다.

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을 사용할 수 있습니다 보려면 : 여기 위에 링크 된 GetEncoding 페이지에서 코드 샘플입니다.

+0

+1 'GetEncodings'에 대해서는이 메소드에 대해 알지 못했지만, 귀하의 코드가 초보 프로그래머를 놀라게 할까 봐 걱정됩니다. – Kamil

+1

@Kamil : 코드 샘플은 연결된 MSDN 기사의 코드 샘플입니다. 그건 내 것이 아니다. :-) MS가 그것이 좋은 표본이라고 생각한다면, 누가 논쟁하겠습니까? :-) 나는 내 인생에서 VB/VB.NET 코드의 두 줄 이상을 쓴 적이 있다고 생각하지 않는다. 코드가 링크 대상에서 왔음을 명확히하기 위해 내 대답을 편집했으며 나에 의해 작성되지 않았습니다. 감사. :-) –

2

인코딩은 모든 인코딩을 의미 할 수 있습니다.

Encoding.Default은 Windows에서 기본 코드 페이지를 반환합니다 (프로그램 실행 Windows의 국가 별 설정에 따라 다름).

Encoding.GetEncoding(1250); 

1250

중앙과 동유럽 라틴 코드 페이지 : 파일이 다른 코드 페이지로 인코딩 된 경우

이 같은, 그 코드 페이지로에서는 StreamReader 인코딩을 설정해야합니다.


나중에 추가 :

당신은 ANSI 형식으로 다시 저장에 대한 질문 - 단지 StreamReader과 동일한 인코딩으로 StreamWriter를 사용합니다. 위키 백과에서 코드 페이지에 대한

몇몇 코드 페이지 예제 및 더 많은 : link

관련 문제