忍者ブログ
ブログ主が仕事や個人的に学んだPC系・プログラミング系(VBAとかHTML)について書いていく備忘録ブログ。 ※ここで記載する内容はあくまで「個人的に」上手くいく内容です。ご使用には十分注意してください
[2] [20] [49] [48] [47] [46] [45] [44] [43
Posted by - 2024.04.28,Sun
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

Posted by 若槻風亜 - 2021.11.09,Tue

こちらで修正した修正コードの内容が
仕事中に欲しくなったので自分用に。


■ファイルのエンコードを取得してファイルを読み込む

Dim MyFile as Strng
Dim Str2 as String

Myfile = "開くファイルのパス"
Call CheckDefaultEncode(MyFile)

    '=======================================================
    ' 文字コードの判別 
    ' 参照↓
    ' https://dobon.net/vb/dotnet/string/getencodingobject.html ←ここのコメント欄
    ' https://dobon.net/vb/dotnet/string/getencodingobject.html
    ' http://nonsoft.la.coocan.jp/SoftSample/VB.NET/SampleCode2Code.html
    '=======================================================
    Sub CheckDefaultEncode(ByVal File0 As String)
        '■変数の設定
        Dim bs As Byte() = IO.File.ReadAllBytes(File0)
        Dim bytCheck As Byte()
        Dim strCheck As String
        Dim Enc as String
        Dim Reader  as  IO.StreamReader
        '■順番にエンコードを読み込み、通常文字列とバイト数を比較して一致したものを読み込み
        For cnt = 1 To 6
            '△処理回数によって対象を変更
            If cnt = 1 Then Enc = "shift_jis"
            If cnt = 2 Then Enc = "euc-jp"
            If cnt = 3 Then Enc = "iso-2022-jp"
            If cnt = 4 Then Enc = "utf-8"
            If cnt = 5 Then Enc = "utf-16"
            If cnt = 6 Then Enc = "UNICODE"
            '△文字列とバイトを読み込み比較。一致したら内容を読み込み
            strCheck = System.Text.Encoding.GetEncoding(Enc).GetString(bs)
            bytCheck = System.Text.Encoding.GetEncoding(Enc).GetBytes(strCheck)
            If bs.SequenceEqual(bytCheck) Then
               '★読み込み(1行ずつ読みたい場合はループで回す)
                Reader = New IO.StreamReader(File0, System.Text.Encoding.GetEncoding(Enc))
                TextBox1.Text = Reader.ReadToEnd
                
                ’★解除
                Reader.Close
                Reader.Dispose
                
                '★読み終わったので処理を抜ける
                Exit For
            End If  '---読み込み値が一致するかどうか
        Next  '---エンコード分処理を回すFor-Next
    End Sub

拍手[0回]

PR
Comments
Post a Comment
Name :
Title :
E-mail :
URL :
Comments :
Pass :   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
プロフィール
HN:
若槻風亜
性別:
女性
職業:
会社員
趣味:
創作、プログラミング
自己紹介:
仕事や個人で学んだことをまとめておきたかったがために備忘録ブログを立ち上げました。
あくまで自分が学んだこと・自分が出来たことなので、ご覧くださる場合は参照レベルでお願いします。
ブログ内検索
カレンダー
03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
P R
最新トラックバック
コガネモチ
フリーエリア



Template by mavericyard*
Powered by "Samurai Factory"
忍者ブログ [PR]