ファイルの暗号化

自家製ファイル暗号化ソフト

filesecurity

2009年くらいに制作したツール(Microsoft C#で制作)ですが 小変更をしたら Windows10でも使えました。

任意のファイルを暗号化し、その単一ファイルにパスワードも練りこまれるので

復元にはパスワードが必要になります。

Microsoft .NET Nativeがデスクトップアプリにも適応できるようになるらしいので

今後に期待。

以下にお手製コードを一部紹介・・

 

  *****************************途中省略*******************************

  /// <summary>
  /// アプリケーションのメイン エントリ ポイントです。
  /// </summary>
        ////SHBrowseForFolder()関数
        //[DllImport("Shell32.dll",CharSet=CharSet.Unicode)]
        //public static extern int SHBrowseForFolder(BrowseInfo lpbi);
        ////lstrcat()関数
        //[DllImport("kernel32",CharSet=CharSet.Unicode)]
        //public static extern int lstrcat(string str1, string str2);
        ////SHGeetPathFromIDList()関数
        //[DllImport("Shell32.dll",CharSet=CharSet.Unicode)]
        //public static extern int SHGetPathFromIDList(int pidl,[MarshalAs(UnmanagedType.LPWStr)] string pszPath);
        ////BrowseInfo
        //[StructLayout(LayoutKind.Sequential)]
        //    public class BrowseInfo 
        //{ 
        //    public int hWndOwner;
        //    public UIntPtr pIDLRoot;
        //    public int pszDisplayName;
        //    public int lpszTitle;
        //    public int ulFlags;
        //    public int lpfnCallback;
        //    public int lParam;
        //    public int iImage;
        //}
        //public const int BIF_BROWSEFORCOMPUTER  = 0x1000;
        //public const int BIF_BROWSEFORPRINTER   = 0x2000;
        //public const int BIF_BROWSEINCLUDEFILES = 0x4000;
        //public const int BIF_RETURNONLYFSDIRS   = 0x0001;
        //public const int BIF_VALIDATE           = 0x0020;
        //public const int BIF_NEWDIALOGSTYLE     = 0x0040;
        ////
  public FileStream FileLoad;
  public FileStream FileSave;
  public CryptoStream CS;
        public ProgressBar pb;
        public bool abort;
        public int DropCount = 0;
        public string[] MultiFiles;
        public string OS = Environment.OSVersion.Version.Major.ToString() + Environment.OSVersion.Version.Minor.ToString();
  [STAThread]
  static void Main() 
  {
            //ビジュアルスタイル有効化
            Application.EnableVisualStyles();
   Application.Run(new Form1());
  }
        // ファイルが読み書きできるか確認
        public bool FileFree(string Path)
        {
                //ディレクトリが指定されていないか確認
                if (Directory.Exists(Path)==false)
                {
                    //ファイルが存在するか確認
                    if (File.Exists(Path) == true)
                    {
                        try
                        {
                            FileStream Chk = new FileStream(Path, FileMode.Open, FileAccess.ReadWrite);
                            Chk.Close();
                            return true;
                        }
                        catch(Exception e){
                                            MessageBox.Show(null,e.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                            return false;
                                          }
                    }
                    else
                    {
                        MessageBox.Show(null, "ファイルが見つかりません。\r\nファイル名\r\n" + Path, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return false;
                    }
                }
                else
                {
                    MessageBox.Show(null, "ファイル以外が指定されました。\r\nディレクトリ名\r\n" + Path, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
        }
        // ファイルを暗号化する
        public void Encrypt(string _FileName,string _Passwd)
        {
            //ファイルが読み書き可能かチェック
            if (FileFree(_FileName) == true)
            {
                    UnicodeEncoding UE = new UnicodeEncoding();
                    byte[] Pass = UE.GetBytes(_Passwd);
                    if (_Passwd.Length > 8)
                    {
                        Pass = UE.GetBytes(_Passwd.Substring(0, 8));
                    }
                    else
                    {
                        string Temp = _Passwd;
                        int Count = 8 - Temp.Length;
                        for (int i = 0; i < Count; i++)
                            Temp = Temp + i;
                        Pass = UE.GetBytes(Temp);
                        Temp = "";
                    }
                    try
                    {
                        //一時ファイルを作成する
                        File.Copy(_FileName, _FileName + ".gtm", true);
                        Application.DoEvents();
                        //Windows7 ファイル占有エラー対策
                        if (OS == "61")
                            Thread.Sleep(1000);
                        //暗号化
                        //暗号化するファイルをロードする
                        FileLoad = new FileStream(_FileName + ".gtm", FileMode.Open, FileAccess.Read);
                        //暗号化したファイルをセーブする
                        FileSave = new FileStream(_FileName, FileMode.Create, FileAccess.Write, FileShare.None);
                        Rijndael Rijn = Rijndael.Create();
                        //Key Size
                        Rijn.KeySize = 256;
                        CS = new CryptoStream(FileSave, Rijn.CreateEncryptor(Pass, Pass), CryptoStreamMode.Write);
                        int lng = 0;
                        long ct = 0;
                        //ファイルのサイズ
                        long FileSize = FileLoad.Length;
                        //10MB Buffer
                        byte[] buf = new byte[10240000];
                        //進行状況バー
                        using (pb = new ProgressBar())
                        {
                            pb.Top = 65;
                            pb.Left = 72;
                            pb.Width = 352;
                            pb.Height = 15;
                            tabControl1.TabPages[1].Controls.Add(pb);
                            //プログレスバーの最大値
                            pb.Maximum = (int)(FileSize / 1024000);
                            while (ct < FileSize)
                            {
                                Application.DoEvents();
                                lng = FileLoad.Read(buf, 0, 10240000);
                                CS.Write(buf, 0, lng);
                                ct = ct + lng;
                                //プログレスバー進行
                                pb.Value = (int)(ct / 1024000);
                            }
                            //ファイルを閉じる
                            CS.Close();
                            FileSave.Close();
                            FileLoad.Close();
                            File.Delete(_FileName + ".gtm");
                            //メモリ解放
                            buf = null;
                            lng = 0;
                            ct = 0;
                            FileSize = 0;
                            GC.Collect();
                        }
                    }
                    //ファイル関係のトラブルが発生した場合
                    catch (System.IO.IOException er)
                    {
                        FileSave.Close();
                        FileLoad.Close();
                        if (File.Exists(_FileName + ".gtm") == true)
                        {
                            //一時ファイルの内容を本来のファイルへ戻す
                            File.Copy(_FileName + ".gtm", _FileName, true);
                            File.Delete(_FileName + ".gtm");
                        }
                        MessageBox.Show(null, er.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //メモリ解放
                        GC.Collect();
                    }
                    //その他のトラブルが発生した場合
                    catch (Exception er)
                    {
                        FileSave.Close();
                        FileLoad.Close();
                        if (File.Exists(_FileName + ".gtm") == true)
                        {
                            //一時ファイルの内容を本来のファイルへ戻す
                            File.Copy(_FileName + ".gtm", _FileName, true);
                            File.Delete(_FileName + ".gtm");
                        }
                        MessageBox.Show(null, er.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //メモリ解放
                        GC.Collect();
                    }
            }
        }
        //ファイルを復号化する
        public void Decrypt(string _FileName,string _Passwd)
        {
                //ファイルが読み書き可能かチェック
                if (FileFree(_FileName) == true)
                {
                    UnicodeEncoding UE = new UnicodeEncoding();
                    byte[] Pass = UE.GetBytes(_Passwd);
                    if (_Passwd.Length > 8)
                    {
                        Pass = UE.GetBytes(_Passwd.Substring(0, 8));
                    }
                    else
                    {
                        string Temp = _Passwd;
                        int Count = 8 - Temp.Length;
                        for (int i = 0; i < Count; i++)
                            Temp = Temp + i;
                        Pass = UE.GetBytes(Temp);
                        Temp = "";
                    }
                    try
                    {
                        //一時ファイルを作成する
                        File.Copy(_FileName, _FileName + ".gtm", true);
                        Application.DoEvents();
                        //Windows7 ファイル占有エラー対策
                        if (OS == "61")
                            Thread.Sleep(1000);
                        //復号化
                        //暗号化するファイルをロードする
                        FileLoad = new FileStream(_FileName + ".gtm", FileMode.Open, FileAccess.Read, FileShare.None);
                        //暗号化したファイルをセーブする
                        FileSave = new FileStream(_FileName, FileMode.Create, FileAccess.Write, FileShare.None);
                        //
                        Rijndael Rijn = Rijndael.Create();
                        //Key Size
                        Rijn.KeySize = 256;
                        CS = new CryptoStream(FileSave, Rijn.CreateDecryptor(Pass, Pass), CryptoStreamMode.Write);
                        int lng = 0;
                        int ct = 0;
                        //ファイルサイズ
                        long FileSize = FileLoad.Length;
                        //10MB Buffer
                        byte[] buf = new byte[10240000];
                        //進行状況バー
                        using (pb = new ProgressBar())
                        {
                            pb.Top = 65;
                            pb.Left = 72;
                            pb.Width = 352;
                            pb.Height = 15;
                            tabControl1.TabPages[2].Controls.Add(pb);
                            //プログレスバーの最大値
                            pb.Maximum = (int)(FileSize / 1024000);
                            while (ct < FileSize)
                            {
                                Application.DoEvents();
                                lng = FileLoad.Read(buf, 0, 10240000);
                                CS.Write(buf, 0, lng);
                                ct = ct + lng;
                                //プログレスバー進行
                                pb.Value = (int)(ct / 1024000);
                            }
                            CS.Close();
                            FileSave.Close();
                            FileLoad.Close();
                            File.Delete(_FileName + ".gtm");
                            //メモリ解放
                            buf = null;
                            lng = 0;
                            ct = 0;
                            FileSize = 0;
                            GC.Collect();
                        }
                    }
                    //復号化に失敗した場合
                    catch (System.Security.Cryptography.CryptographicException er)
                    {
                        FileSave.Close();
                        FileLoad.Close();
                        if (File.Exists(_FileName + ".gtm") == true)
                        {
                            //一時ファイルの内容を本来のファイルへ戻す
                            File.Copy(_FileName + ".gtm", _FileName, true);
                            File.Delete(_FileName + ".gtm");
                        }
                        if (er.Message.IndexOf("パディング") != -1)
                            MessageBox.Show(null, "復号化できません。パスワードが違います。\r\nファイル名\r\n" + _FileName, "認証", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        if (er.Message.IndexOf("データの長さが無効") != -1)
                            MessageBox.Show(null, "暗号化ファイルではありません。\r\nファイル名\r\n" + _FileName, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //メモリ解放
                        GC.Collect();
                    }
                    //その他のトラブルが発生した場合。
                    catch (Exception a)
                    {
                        FileSave.Close();
                        FileLoad.Close();
                        if (File.Exists(_FileName + ".gtm") == true)
                        {
                            //一時ファイルの内容を本来のファイルへ戻す
                            File.Copy(_FileName + ".gtm", _FileName, true);
                            File.Delete(_FileName + ".gtm");
                        }
                        MessageBox.Show(null, a.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        //メモリ解放
                        GC.Collect();
                    }
                }
        }
  private void button1_Click(object sender, System.EventArgs e)
  {
   openFileDialog1.Filter="すべてのファイル|*.*";
   openFileDialog1.Title="暗号化対象ファイル";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                MultiFiles = new string[] { };
                DropCount = 1;
                textBox1.Text = openFileDialog1.FileName;
                button2.Focus();
            }
  }
  private void button2_Click(object sender, System.EventArgs e)
  {
            //暗号化
    tabControl1.Enabled=false;
    Form2 PasswdDlg = new Form2();
    PasswdDlg.Owner=this;
                PasswdDlg.TopMost = true;
    //パスワードの処理
    if (PasswdDlg.ShowDialog()==DialogResult.OK)
                {
                    //ファイルが一つの場合
                    if (DropCount < 2)
                        Encrypt(textBox1.Text, PasswdDlg.Passwd);
                    else
                    {
                        //ファイルが複数の場合
                        for (int i=0; i < DropCount; i++ )
                        {
                            Application.DoEvents();
                            Encrypt(MultiFiles[i],PasswdDlg.Passwd);
                        }
                    }
                       
    }
    tabControl1.Enabled=true;
    textBox1.Clear();
                MultiFiles = new string[]{};
                DropCount = 0;
   
  }
  private void button4_Click(object sender, System.EventArgs e)
  {
   openFileDialog1.Filter = "暗号化されたファイル|*.*";
   openFileDialog1.Title = "復号化対象ファイル";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                MultiFiles = new string[] { };
                DropCount = 1;
                textBox2.Text = openFileDialog1.FileName;
                button3.Focus();
            }
  }
  private void button3_Click(object sender, System.EventArgs e)
  {
            //復号化
    tabControl1.Enabled=false;
    Form2 PasswdDlg = new Form2();
    PasswdDlg.Owner=this;
                PasswdDlg.TopMost = true;
    if (PasswdDlg.ShowDialog()==DialogResult.OK)
    {
                    //ファイルが一つの場合
                    if (DropCount < 2)
                        Decrypt(textBox2.Text, PasswdDlg.Passwd);
                    else
                    {
                        //ファイルが複数の場合
                        for (int i = 0; i < DropCount; i++)
                        {
                            Decrypt(MultiFiles[i], PasswdDlg.Passwd);
                        }
                    }
    }
    tabControl1.Enabled=true;
                textBox2.Clear();
                MultiFiles = new string[] { };
                DropCount = 0;
  }
  private void button5_Click(object sender, System.EventArgs e)
  {
            textBox6.Text = "ここに結果が表示されます";
            openFileDialog1.Filter = "ハッシュ値計算対象ファイル|*.*";
            openFileDialog1.Title = "ハッシュ値計算対象ファイル";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = openFileDialog1.FileName;
                button7.Focus();
            }
  }
  private void button6_Click(object sender, System.EventArgs e)
  {
            if (textBox6.Text.Length > 0 & textBox6.Text != "ここに結果が表示されます")
            {
                Clipboard.SetDataObject(textBox6.Text, true);
                MessageBox.Show(null, "クリップボードへコピーしました。","ハッシュ値", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
  }
  private void button7_Click(object sender, System.EventArgs e)
  {
            try
            {
                if (File.Exists(textBox4.Text))
                {
                    button7.Enabled = false;
                    Application.DoEvents();
                    //MessageBox.Show((string)comboBox2.Text);
                    //MD5
                    if(comboBox2.Text == "MD5")
                    {
                        FileStream fs = new FileStream(textBox4.Text, FileMode.Open, FileAccess.Read, FileShare.None);
                        MD5 md = MD5.Create();
                        byte[] hash = md.ComputeHash(fs);
                        fs.Close();
                        md.Clear();
                        StringBuilder sb = new StringBuilder();
                        foreach (byte b in hash)
                        {
                            Application.DoEvents();
                            sb.Append(b.ToString("X2"));
                        }
                        textBox6.Text = sb.ToString();
                    }
                    //SHA1
                    if (comboBox2.Text == "SHA1")
                    {
                        FileStream fs = new FileStream(textBox4.Text, FileMode.Open, FileAccess.Read, FileShare.None);
                        SHA1 sha1 = new SHA1Managed();
                        byte[] hash = sha1.ComputeHash(fs);
                        fs.Close();
                        sha1.Clear();
                        StringBuilder sb = new StringBuilder();
                        foreach (byte b in hash)
                        {
                            Application.DoEvents();
                            sb.Append(b.ToString("X2"));
                        }
                        textBox6.Text = sb.ToString();
                    }
                    //SHA256
                    if (comboBox2.Text == "SHA256")
                    {
                        FileStream fs = new FileStream(textBox4.Text, FileMode.Open, FileAccess.Read, FileShare.None);
                        SHA256 sha256 = new SHA256Managed();
                        byte[] hash = sha256.ComputeHash(fs);
                        fs.Close();
                        sha256.Clear();
                        StringBuilder sb = new StringBuilder();
                        foreach (byte b in hash)
                        {
                            Application.DoEvents();
                            sb.Append(b.ToString("X2"));
                        }
                        textBox6.Text = sb.ToString();
                    }
                    //SHA384
                    if (comboBox2.Text == "SHA384")
                    {
                        FileStream fs = new FileStream(textBox4.Text, FileMode.Open, FileAccess.Read, FileShare.None);
                        SHA384 sha384 = new SHA384Managed();
                        byte[] hash = sha384.ComputeHash(fs);
                        fs.Close();
                        sha384.Clear();
                        StringBuilder sb = new StringBuilder();
                        foreach (byte b in hash)
                        {
                            Application.DoEvents();
                            sb.Append(b.ToString("X2"));
                        }
                        textBox6.Text = sb.ToString();
                    }
                    //SHA512
                    if (comboBox2.Text == "SHA512")
                    {
                        FileStream fs = new FileStream(textBox4.Text, FileMode.Open, FileAccess.Read, FileShare.None);
                        SHA512 sha512 = new SHA512Managed();
                        byte[] hash = sha512.ComputeHash(fs);
                        fs.Close();
                        sha512.Clear();
                        StringBuilder sb = new StringBuilder();
                        foreach (byte b in hash)
                        {
                            Application.DoEvents();
                            sb.Append(b.ToString("X2"));
                        }
                        textBox6.Text = sb.ToString();
                    }
                    button7.Enabled = true;
                }
                else
                {
                    MessageBox.Show(null,"ファイルが見つかりません。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception er)
            {
                MessageBox.Show(null, "ハッシュ値計算に失敗しました。\r\n"+er.Message, "エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                button7.Enabled = true;
                textBox6.Text = "ここに結果が表示されます";
                textBox4.Text = "";
            }
  }
        private void textBox4_TextChanged(object sender, EventArgs e)
        {
        }
        private void textBox4_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }
        private void textBox4_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop) & ((string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1)
            {
                string[] path = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (File.Exists(path[0]))
                    textBox4.Text = path[0];
            }
            else
            {
                textBox4.Text = "マルチファイル...";
                MessageBox.Show(null, "ここには複数のファイルはドロップできません。", "MD5ハッシュ値計算", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                textBox4.Text = "";
            }
        }
        private void textBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else 
                e.Effect = DragDropEffects.None;
        }
        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            //暗号化ドロップ
            MultiFiles = new string[] { };
            DropCount = 0;
            //ファイルが1つの場合
            if (e.Data.GetDataPresent(DataFormats.FileDrop) & ((string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1)
            {
                DropCount = ((string[])e.Data.GetData(DataFormats.FileDrop)).Length;
                string[] path = (string[])e.Data.GetData(DataFormats.FileDrop);
                if(File.Exists(path[0]))
                textBox1.Text = path[0];
                button2.PerformClick();
            }
            else
                //複数のファイルの場合
                if (e.Data.GetDataPresent(DataFormats.FileDrop) & ((string[])e.Data.GetData(DataFormats.FileDrop)).Length > 1)
                {
                    DropCount = ((string[])e.Data.GetData(DataFormats.FileDrop)).Length;
                    MultiFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
                    textBox1.Text = "マルチファイル...";
                    button2.PerformClick();
                }
        }
        private void textBox2_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }
        private void textBox2_DragDrop(object sender, DragEventArgs e)
        {
            //復号化ドロップ
            MultiFiles = new string[] { };
            DropCount = 0;
            //ファイルが1つの場合
            if (e.Data.GetDataPresent(DataFormats.FileDrop) & ((string[])e.Data.GetData(DataFormats.FileDrop)).Length == 1)
            {
                DropCount = ((string[])e.Data.GetData(DataFormats.FileDrop)).Length;
                string[] path = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (File.Exists(path[0]))
                    textBox2.Text = path[0];
                    button3.PerformClick();
            }
            else
            //複数のファイルの場合
            if (e.Data.GetDataPresent(DataFormats.FileDrop) & ((string[])e.Data.GetData(DataFormats.FileDrop)).Length > 1)
            {
                DropCount = ((string[])e.Data.GetData(DataFormats.FileDrop)).Length;
                MultiFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
                textBox2.Text = "マルチファイル...";
                button3.PerformClick();
            }
        }
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            //ドライブ一覧を列挙
            if (e.TabPage.Text == "ファイル検索")
            {
                comboBox1.Items.Clear();
                comboBox1.Items.Add("列挙中...");
                comboBox1.SelectedIndex = 0;
                Application.DoEvents();
                comboBox1.Items.Clear();
                foreach (DriveInfo drive in DriveInfo.GetDrives())
                {
                    if (drive.Name != "A:\\" & drive.Name != "B:\\" & drive.DriveType.ToString() == "Fixed" | drive.DriveType.ToString() == "Network")
                        comboBox1.Items.Add(drive.Name);
                }
                comboBox1.SelectedIndex = 0;
            }
            if (e.TabPage.Text == "ハッシュ値計算")
            {
                textBox4.Text = "";
                textBox6.Text = "ここに結果が表示されます";
                comboBox2.SelectedIndex = 0;
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            if (textBox5.Text.Length > 1)
            {
                button8.Enabled = false;
                textBox5.Enabled = false;
                abort = false;
                //選択されたドライブ
                string Drive = comboBox1.Text;
                //検索結果ダイアログ
                Form3 srh = new Form3();
                ListViewItem item = new ListViewItem();
                srh.Owner = this;
                srh.Show();
                srh.label1.Text = "ファイルを探しています・・・";
                //検索
                Search(Drive, textBox5.Text, srh.listView1, srh.label1);
                if (!abort)
                {
                    srh.label1.Text = "ファイルの検索結果  一覧を選択するとファイルが存在するフォルダを開きます。";
                    srh.button1.Enabled = false;
                }
                else
                {
                    srh.label1.Text = "検索を中止しました。";
                }
                abort = false;
            }
            else
                MessageBox.Show(null, "検索候補が入力されていません。\r\n2文字以上入力してください。", "ファイル検索", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        //検索関数
        private void Search(string _Drive, string _Name,ListView _ListView,Label _l)
        {
            try
            {
                foreach (string f in Directory.GetFiles(_Drive))
                {
                    Application.DoEvents();
                    //検索中止フラグがtrueの場合中止
                    if (abort){
                        _l.Text = "検索を中止しています・・・";
                        return;
                    }
                    //ルートディレクトリ
                    FileInfo fi = new FileInfo(f);
                    //1文字でも一致した場合
                    if (fi.Name.IndexOf(_Name, StringComparison.CurrentCultureIgnoreCase) != -1 & radioButton1.Checked)
                    {
                        //検索 結果を追加
                        ListViewItem _Item = new ListViewItem();
                        //ファイル名
                        _Item.Text = fi.Name;
                        //ディレクトリ名
                        _Item.SubItems.Add(fi.DirectoryName);
                        //サイズ
                        _Item.SubItems.Add(fi.Length.ToString());
                        //MD5
                        try
                        {
                                FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
                                MD5 md = MD5.Create();
                                byte[] hash = md.ComputeHash(fs);
                                fs.Close();
                                md.Clear();
                                StringBuilder sb = new StringBuilder();
                                foreach (byte b in hash)
                                {
                                    Application.DoEvents();
                                    sb.Append(b.ToString("X2"));
                                }
                                _Item.SubItems.Add(sb.ToString());
                        }
                        catch (Exception er)
                        {
                            _Item.SubItems.Add(er.Message);
                        }
                        //
                        //MD5 結果をリストへ追加
                        _ListView.Items.Add(_Item);
                    }
                    //完全一致した場合
                    if (fi.Name.ToLower() == _Name.ToLower() & radioButton2.Checked)
                    {
                        //検索 結果を追加
                        ListViewItem _Item = new ListViewItem();
                        //ファイル名
                        _Item.Text = fi.Name;
                        //ディレクトリ名
                        _Item.SubItems.Add(fi.DirectoryName);
                        //サイズ
                        _Item.SubItems.Add(fi.Length.ToString());
                        //MD5
                        try
                        {
                            FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
                            MD5 md = MD5.Create();
                            byte[] hash = md.ComputeHash(fs);
                            fs.Close();
                            md.Clear();
                            StringBuilder sb = new StringBuilder();
                            foreach (byte b in hash)


                            ******以下掲載コードの省略******

0

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です