SendKey.Send(变量)在其他计算机上不起作用

时间:2020-06-09 13:49:36

标签: c# windows forms sendkeys

有人告诉我他需要一个程序来在另一个应用程序的文本框中键入名称。我无法从应用程序获取API。他必须键入在excel文件中列出的名称。输入每个名字后,他必须按 enter 来创建类似于YouTube或stackoverflow中的标签的内容。 除非有某种我不知道的代码,否则别无选择。这就是为什么我寻求帮助。 我考虑过看看这些,但没有答案是解决方案。
Why does SendKey.Send() only work once in a while?->应用设置已添加到配置文件。
SendKey.Send() Not working->我必须输入变量

我成功创建了此应用程序。我在笔记本电脑和台式机上都对其进行了测试。它对两个都有效。 它还可以在家庭成员的笔记本电脑上使用。这个家庭成员是普通的计算机用户,不会编写任何代码。

程序目标:
键入在excel文件中列出的名称,并将它们与任何值分隔到另一个应用程序/站点中。

例如:
值:
琼斯
福尔摩斯
琼森
波特

分隔值=“;”

输出:
琼斯;霍姆斯;琼森;波特

问题是:
该应用程序无法在随机计算机(笔记本电脑和台式电脑)上运行。该应用程序将一直运行,直到必须执行SendKey.Send命令为止。但是几秒钟后,应用程序确认整个过程已经执行。此确认在我的应用程序中通过消息框进行了硬编码。在确认之前,它仅键入名字的第一个字符。

所有提到的计算机都是Windows 10。 我已经用SendKeys.Send和SendKeys.SendWait尝试过。

在此先感谢您的帮助!


代码:(通过简单的按钮单击触发)

List<string> Values = new List<string>(); //These are set by another method --> it's not empty and works fine
string Split = string.Empty;
switch (strSplitValue)
{
    case "ENTER":
        Split = "";
    break;
    case "SPATIE":
        Split = " ";
    break;
    case ",":
        Split = ", ";
    break;
    case ";":
        Split = ";";
    break;
    case "/":
        Split = "/";
    break;
    case "\\":
        Split = "\\";
    break;
    default:
        Split = strSplitValue;
    break;
}
//A messagebox pops up here to clarify that the user has 8s to click with the mouse on the place where the application has to type its values. After pessed ok, it goes on.
//This messagebox always works. The program fails from the moment it executes SendKeys.Send
try {
        foreach (string x in Values)
        {
            string strSendkeys = x + Split;
            SendKeys.Send(strSendkeys);
        if (strSplitValue == "ENTER")
            {
                Thread.Sleep(1200);
                SendKeys.Send("{ENTER}");
                Thread.Sleep(200);
            }
    }
    MessageBox.Show("Task succeeded!", "Succeeded!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch {
    MessageBox.Show("Something went wrong.");
}

1 个答案:

答案 0 :(得分:0)

显然,我设法通过SendKeys.SendWait使它起作用。 第一次尝试成功了,但没有成功,后来又继续永久起作用。