[英]Activate Window and Send Input using SetForegroundWindow and SendKeys in C#
[英]C# SetforegroundWindow to VB.Net then SendKeys
我正在自動化一些流程。 我運行VB.net應用程序,需要輸入密碼,依次進入“登錄”按鈕,發送回車,進入“運行負載”按鈕並按Enter。 SendKey部分似乎不是問題; 它正在尋找合適的應用程序來集中精力。 我希望表格可以使用.TopMost可以幫上忙,但不幸的是情況並非如此。
[DllImport("User32")]
private static extern int SetForegroundWindow(int hwnd);
public void runDailyWS()
{
string homeWSPath = "D:\\...";
var home = Process.Start(homeWSPath);
var procName = "DataLoad Home Test - Copy";
Process[] p = Process.GetProcessesByName(procName);
// Activate the first application we find with this name
if (p.Count() > 0)
{
SetForegroundWindow(p[0].MainWindowHandle);
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("password");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{ENTER}");
}
///Did not post rest of code after this point as it's unnecessary
我嘗試了幾種不同的方法將VB.net應用程序/進程置於最前面,奇怪的是,有時它可以工作,但有時還不夠好。 有人有什么想法嗎? 我也看過前6個Google搜索結果。 謝謝。
編輯:為澄清起見,此代碼啟動了VB.net應用程序,而我正在嘗試自動執行擊鍵操作以輸入憑據,然后按Enter鍵啟動應用程序。 為了使擊鍵實際注冊,VB.net應用程序必須是“活動”窗口。 也將我的代碼更改為有效的代碼(有效1/7次嘗試)。
我添加了Thread.Sleep(2000);
因為代碼只是在應用程序有機會完全加載之前就輸入了擊鍵。 我不知道此解決方案是否會對其他人有所幫助,但這絕對是要考慮的事情。 感謝那些貢獻者。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.