首页 > 解决方案 > 在执行下一个 C# 之前等待用户再次复制某些内容的剪贴板

问题描述

我想做以下,但我不知道如何开始做我想要的。

1. 检查md5是否被复制

2. 然后清除剪贴板

3.循环检查剪贴板是否仍然为空,然后通知将您想要散列的内容复制到 md5

4.在剪贴板非空后进行哈希处理的代码,毕竟显示了 md5 哈希值的通知,什么被复制了。

这是我现在的代码

        switch ((clipboardText))
        {
            case "md5":
                ShowNotification("MD5 HASHER", "Copy what you want to hash to md5");
                {
                    ClearClipboard();
                    string input = GetTextClipboard();
                    // step 1, calculate MD5 hash from input
                    MD5 md5 = MD5.Create();
                    byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
                    byte[] hash = md5.ComputeHash(inputBytes);
                    // step 2, convert byte array to hex string
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < hash.Length; i++)
                    {
                        sb.Append(hash[i].ToString("X2"));
                    }
                    ShowNotification("md5 hash", sb.ToString());
                }
                break;
        }

标签: c#while-loopmd5clipboard

解决方案


要检查剪贴板的值,请调用该方法:

 public static string GetTextClipboard()
    {
        string txtReturn = null;
        Thread staThread = new Thread(
            delegate ()
            {
                try
                {
                    txtReturn = Clipboard.GetText();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            });
        staThread.SetApartmentState(ApartmentState.STA);
        staThread.Start();
        staThread.Join();
        return txtReturn;
    }

清除剪贴板:

public static void ClearClipboard()
        {
            Thread staThread = new Thread(
                delegate ()
                {
                    try
                    {
                        Clipboard.Clear();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                });
            staThread.SetApartmentState(ApartmentState.STA);
            staThread.Start();
            staThread.Join();
        }

测试它:

 while (true)
            {
                clipboardText = GetTextClipboard();
                switch ((clipboardText))
                {
                    case "md5":
                        ShowNotification("MD5 HASHER", "Copy what you want to hash to md5");
                        {
                            string source = GetTextClipboard();
                            using (MD5 md5Hash = MD5.Create())
                            {
                                string hash = GetMd5Hash(md5Hash, source);
                                ShowNotification("", "The MD5 hash is" + hash + ".");
                                ShowNotification("", "Verifying the hash...");
                                if (VerifyMd5Hash(md5Hash, source, hash))
                                {
                                    ShowNotification("", "The hashes are the same.");
                                }
                                else
                                {
                                    ShowNotification("", "The hashes are not same.");
                                }
                            }
                        }
                        break;

                }
Thread.Sleep (2000); 
            }

推荐阅读