c# - 在执行下一个 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;
}
解决方案
要检查剪贴板的值,请调用该方法:
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);
}
推荐阅读
- php - 站点地图的 WordPress 分页
- java - How large should my list of objects be to warrant the use of java 8's parallelStream?
- arrays - 如何使用密钥从 json 中提取对象?
- vba - VBA Word:您可以将类、标签或名称分配给范围以独立于表格格式访问它们吗?
- spring - Spring Boot 2 数据 JPA:事务不回滚
- python - 如何对列表元素的值求和,哪些元素的值来自熊猫中的 anthor 数据框?
- android - 强制 LinearLayout 子级使用所有可用空间,但至少足以容纳其内容
- azure - K8 Ingress 没有端点
- c++ - 操作失败,没有当前记录。异常类:CDBException,错误:1007
- java - 将文件生成到不同的包 pom.xml