I have tried every thing but it wont work...This program get md5 hashes of files.
Here is all of my source code, please help me:
ERROR:
When trying to put data of MD5Hashes the program automatically sends an empty slot in the array, I even tried to push it one forward with the copyTo function but it didn't work.
1)The main problem is when the data is sent to the GetMD5HashFromFile -> it gets an empty filepath..
look at lines 41-51~ I think that the problem is there.
2) I want an array that could hold more than 100 slots I want it to grow regarding the folder files size...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;

namespace checksum_test
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //init start
        public string foldername = "";
        public Array md5p = new String[100];
        public bool oktoCheck = false;
        //init end

        /*Chcek MD5 start*/
        private void button1_Click(object sender, EventArgs e)
        {
            if (oktoCheck)
            {
                object[] found = new object[listView1.Items.Count];

                listView1.Items.CopyTo(found, 0);
                MessageBox.Show(found.ToString());
                if (found.Length == 0)
                {
                    MessageBox.Show("empty"); //reset open folder browser 
                }
                ListViewItem i = new ListViewItem("column1");
                Array targetmd5p = new String[101];
                md5p.CopyTo(targetmd5p, 1);//fix this
                
                foreach (string md5t in targetmd5p)
                {
                        string md5value;
                        MessageBox.Show(md5t);
                        md5value = GetMD5HashFromFile(md5t);
                        MessageBox.Show(md5value);
                        i.SubItems.Add(md5value);
                }

            }
            else { MessageBox.Show("Please open a directory first."); }
        }
        /*Chcek MD5 button end*/
        /*GetMD5HashFromFile start*/
        protected string GetMD5HashFromFile(string filePath)
        {
            FileStream file = new FileStream(filePath, FileMode.Open);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }
        /*GetMD5HashFromFile end*/
        /*FolderBrowser start*/
        private void button2_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.ShowNewFolderButton = false;
            DialogResult result = this.folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                oktoCheck = true;
                foldername = this.folderBrowserDialog1.SelectedPath;
                int i = 0;
                
                foreach(string f in Directory.GetFiles(foldername)){
                   
                    this.listView1.Items.Add(f.ToString());
                    md5p.SetValue(f,0+i);
                    i++;
                }
            }
        }
        /*FolderBrowser end*/

    }
}

Recommended Answers

All 2 Replies

Delete post I have solved it.

Hi!

I think the path is not Valid when it is trying to open it, you should try like this:

/*GetMD5HashFromFile start*/
        protected string GetMD5HashFromFile(string filePath)
        {
           StringBuilder sb = new StringBuilder(); 
           [B]if(IO.File.Exists(filePath))[/B] {
            FileStream file = new FileStream(filePath, FileMode.Open);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();
              for (int i = 0; i < retVal.Length; i++)
              {
                  sb.Append(retVal[i].ToString("x2"));
              }
            }
            return sb.ToString();
        }
        /*GetMD5HashFromFile end*/

and check are you getting "" as a return from function call, then treat it accordingly with a Message like "Path invalid" etc...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.