如何在我的代码中调用此方法 [英] How do I call this method in my code

查看:73
本文介绍了如何在我的代码中调用此方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开展一个项目,我的朋友告诉我我需要做些什么才能取得进展,但他没有告诉我如何这样做,现在我不知道该做什么就盯着屏幕。



这就是他所说的





在你的点击处理程序中你可以调用函数。那么为什么不将
函数添加到表单类中呢?然后调用它传递你填写文件名文本框时得到的
的文件名?将函数
的结果存储到散列文本中。





我要做的是计算MD5 Hash来自我在PC上选择的文件。这就是我的代码。



 命名空间 MD5_Hash 

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

public string MD5HashFile( string fn)
{
byte [] hash = MD5.Create()。ComputeHash(File。 ReadAllBytes(FN));
return BitConverter.ToString(hash).Replace( - );
}

private void lblTitle_Load( object sender,EventArgs e)
{

}

private void scanButton_Click( object sender,EventArgs e)
{
// 就在这里
}

private void browseButton_Click( object sender,EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
txtFilePath.Text = (ofd.FileName);

}
}
}
}





基本上我试图使用

 scanButton 

从我从

 private void browseButton_Click  





我有点想知道他在说什么,但不完全是因为我不熟悉密码学。 />


如果你可以简化他要求我做的事情,我会非常感激!



我尝试了什么:



我试过调用哈希但是我知道我这样做完全错了

解决方案

从文本框中读取文件名:

  string  path = txtFilePath。文字; 



检查文件是否存在:

  if (!File.Exists(path))
{
...向用户报告问题。
return ;
}



然后只需调用您的方法:

 txtHashValue.Text = MD5HashFile(path) ; 

很简单,是吗?


这是我得到的结论,感谢OrigianalGriff





  //   ------- --------我仍然不知道这些线路做了什么--------------------  
public string MD5HashFile( string fn)
{
byte [] hash = MD5.Create()。ComputeHash(File.ReadAllBytes(fn));
return BitConverter.ToString(hash).Replace( - );

}

// ------- --------我仍然不知道这些线路做什么-------------------- ^

< span class =code-keyword> private void lblTitle_Load( object sender,EventArgs e )
{

}



private void scanButton_Click( object sender,EventArgs e)
{

// 创建一个包含要扫描的文件值的textBox的路径
< span class =code-keyword> string path = txtFilePath.Text;

// 如果文本框中有东西要扫描,我们需要确保它的这样做。
if (!File.Exists(path))
{
// ...向用户报告问题。
return ;

}
else
{
MessageBox.Show( 扫描完成);
}

// 在我们之前声明的路径中显示计算出的MD5哈希值
hashDisplay.Text = MD5HashFile(path);


}





再次感谢OriginalGriff解释!


I am currently working on a project and my friend told me what I need to do to progress but he didnt tell me how to so now im staring into the screen without knowing what to do.

This is what he said


Within your click handler you can call functions. So why not add that
function to your form class. Then call it passing in the filename that
you got when you filled in the filename textbox? Store the result of
the function into the text for the hash.



What I am trying to do is to compute the MD5 Hash from a file that I select on my PC. This is what my code looks like.

namespace MD5_Hash

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

        public string MD5HashFile(string fn)
        {
            byte[] hash = MD5.Create().ComputeHash(File.ReadAllBytes(fn));
            return BitConverter.ToString(hash).Replace("-", "");
        }

        private void lblTitle_Load(object sender, EventArgs e)
        {

        }

        private void scanButton_Click(object sender, EventArgs e)
        {
            //Right here
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtFilePath.Text = (ofd.FileName);

            }
        }
    }
}



basically im trying to use the

scanButton

to compute the MD5 Hash from the file that I get from

private void browseButton_Click




I kinda have an idea of what he is talking about but not exactly because I am new to Cryptography.

If you could simplify what he is asking me to do I would be VERY greatful!

What I have tried:

I've tried calling the hash but I know I am doing it completly wrong

解决方案

Read the filename from your textbox:

string path = txtFilePath.Text;


Check the file exists:

if (!File.Exists(path))
   {
   ... report problem to user.
   return;
   }


Then just call your method:

txtHashValue.Text = MD5HashFile(path);

Simple enough, yes?


Here is what I came to conclusion with Thanks to OrigianalGriff


//---------------I STILL DONT UDNERSTAND WHAT THESE LINES DO--------------------
public string MD5HashFile(string fn)
{
    byte[] hash = MD5.Create().ComputeHash(File.ReadAllBytes(fn));
    return BitConverter.ToString(hash).Replace("-", "");

}

//---------------I STILL DONT UDNERSTAND WHAT THESE LINES DO--------------------^

private void lblTitle_Load(object sender, EventArgs e)
{

}



private void scanButton_Click(object sender, EventArgs e)
{

    //Create a path to the textBox that holds the value of the file that is going to be scanned
    string path = txtFilePath.Text;

    //if there is something in the textbox to scan we need to make sure that its doing it.
    if (!File.Exists(path))
    {
                            // ... report problem to user.
      return;

    }
    else
    {
        MessageBox.Show("Scan Complete");
    }

    //Display the computed MD5 Hash in the path we declared earlier
    hashDisplay.Text = MD5HashFile(path);


}



Thanks again OriginalGriff for explaining!


这篇关于如何在我的代码中调用此方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆