I am facing problem to automate a desktop aplication this code work's only for 2 or 3 days after this send error is access denied but forground windows is focused. and activated ....i run this as admin.and also tried sendkeys.send,sendkey.sendwait.. but problem remains same....
C#
namespace WindowsFormsApplication8C#
{ public partial class Form1 : FormC#
{ public Form1()C#
{C#
InitializeComponent();C#
}C#
[DllImport("User32.dll", SetLastError = true)]C#
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);C#
private void button1_Click(object sender, EventArgs e)C#
{C#
tryC#
{C#
Process myproc= Process.GetProcessesByName("name").FirstOrDefault();C#
if (myproc!= null)C#
{ var ffCondition = new PropertyCondition(AutomationElement.ProcessIdProperty, myproc.Id);C#
var ffWindows = AutomationElement.RootElement.FindAll(TreeScope.Children, ffCondition);C#
var soWindow = ffWindows.CastAutomationElement>().FirstOrDefault(w => w.Current.Name.Contains("title of window "));C#
if (soWindow != null)C#
{C#
var soWindowPattern = soWindow.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;C#
if (soWindowPattern != null)C#
{C#
// Restore window (activating it).C#
soWindowPattern.SetWindowVisualState(WindowVisualState.Normal);C#
Thread.Sleep(100);C#
// Close window. //soWindowPattern.Close();C#
SendKeys.Send("{f1}");C#
//System.Windows.Forms.SendKeys.SendWait("{f1}");C#
//string send = System.Windows.Input.Key.F1.ToString();C#
//SendKeys.SendWait(send);C#
var soWindow1 = ffWindows.CastAutomationElement>().FirstOrDefault(w => w.Current.Name.Contains("another child window "));C#
}C#
}C#
}C#
}C#
catch (Exception ex)C#
{C#
MessageBox.Show(ex.ToString());C#
}C#
}C#
}C#
}
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 16, 2019, 5:38 AM EDT