Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用c#wpf在文本框中显示哈希字符串_C#_Wpf_Textbox - Fatal编程技术网

无法使用c#wpf在文本框中显示哈希字符串

无法使用c#wpf在文本框中显示哈希字符串,c#,wpf,textbox,C#,Wpf,Textbox,我无法在文本框中显示MD5哈希结果。使用命令行,代码运行良好。代码如下所示: private void textBox1_TextChanged(object sender, System.EventArgs e) { textBox1.Text = GetMD5HashFromFile(); } private static string GetMD5HashFromFile() { FileStream file = new File

我无法在文本框中显示MD5哈希结果。使用命令行,代码运行良好。代码如下所示:

private void textBox1_TextChanged(object sender, System.EventArgs e)
    {
        textBox1.Text = GetMD5HashFromFile();
    }

private static string GetMD5HashFromFile()
    {
        FileStream file = new FileStream(@"C:\Desktop\test.txt", FileMode.Open);
        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.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();
    }
private void textBox1\u TextChanged(对象发送者,System.EventArgs e)
{
textBox1.Text=GetMD5HashFromFile();
}
私有静态字符串GetMD5HashFromFile()
{
FileStream file=newfilestream(@“C:\Desktop\test.txt”,FileMode.Open);
System.Security.Cryptography.MD5 MD5=新的System.Security.Cryptography.MD5CryptoServiceProvider();
字节[]retVal=md5.ComputeHash(文件);
file.Close();
StringBuilder sb=新的StringBuilder();
for(int i=0;i

我想计算散列并直接在文本框中显示散列字符串。我的编程概念是错误的还是缺少了一些步骤?谢谢

当用户在文本框中输入文本时(或当
text
属性因任何其他原因更改时),调用
TextChanged
事件处理程序。将哈希字符串指定给
文本
属性的位置错误

您可以将其放在其他地方,可能是在按钮单击处理程序中:

<StackPanel>
    <TextBox x:Name="textBox1"/>
    <Button Content="Set Text" Click="ButtonClick"/>
</StackPanel>

private void ButtonClick(object sender, RoutedEventArgs e)
{
    textBox1.Text = GetMD5HashFromFile();
}

私有无效按钮单击(对象发送者,路由目标)
{
textBox1.Text=GetMD5HashFromFile();
}

那么您的确切问题是什么?您正在修改textbox Changed事件中的textbox?这可能会导致一个讨厌的循环。@Djerry当我运行应用程序时,文本框中没有显示md5哈希结果。代码有什么问题吗?或者我如何进行更改?谢谢Clemens。它是这样工作的,但是不必点击按钮就可以显示结果吗?当然可以。按钮只是一个例子。您可以在任意位置或多或少地分配
Text
属性,但在同一文本框的TextChanged处理程序中除外。为了在应用程序启动后立即显示哈希字符串,您可以将赋值放在主窗口的构造函数中,就在
InitializeComponent
之后。以防您只想显示字符串,但不想对其进行编辑,你应该使用
TextBlock
而不是
TextBox
。非常感谢克莱门斯!