C# using Sendkey function to send a key to another application
2014-03-30 21:03
369 查看
If notepad is already started, you should write:
If you want to start
The only situation in which the code may not work is when
// import the function in your class [DllImport ("User32.dll")] static extern int SetForegroundWindow(IntPtr point); //... Process p = Process.GetProcessesByName("notepad").FirstOrDefault(); if( p != null) { IntPtr h = p.MainWindowHandle; SetForegroundWindow(h); SendKeys.SendWait("k"); }
GetProcessesByNamereturns an array of processes, so you should get the first one (or find the one you want).
If you want to start
notepadand send the key, you should write:
Process p = Process.Start("notepad.exe"); p.WaitForInputIdle(); IntPtr h = p.MainWindowHandle; SetForegroundWindow(h); SendKeys.SendWait("k");
The only situation in which the code may not work is when
notepadis started as Administrator and your application is not.
相关文章推荐
- How to send raw data to a printer by using Visual C# .NET
- Send Push Notifications to iOS Devices using Xcode 8 and Swift 3, APNs Auth Key
- Interview Q&A - Write a C# console application to implement a string replacement function
- Another way to retrieve a custom key's value from web.config in web form application
- How to send Email using C#
- How To Implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET
- kubuntu之another application seems to be using the package system at this time问题即muon无法安装更新的问题
- How to Use .Net Remoting Using C#
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- Android跨进程事件注入 Injecting to another application requires INJECT_EVENTS permission
- Agent admitted failure to sign using the key
- 关于ssh报错:Agent admitted failure to sign using the key.
- Agent admitted failure to sign using the key
- 解决 Agent admitted failure to sign using the key 问题 with ssh
- How to update multiple columns of one table using values from another table?
- 错误:created a ThreadLocal with key of type ……but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
- 解决 Agent admitted failure to sign using the key 问题 with ssh
- How to crack a wireless WEP key using AIR Crack
- how to send a SIP message using OSIP stack