C#-SendKeys方法的使用

模拟键盘CTRL+S 的输入

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Test_C_Shap
{
    static class Program
    {
        [DllImport("user32.dll")]
        public static extern int SetForegroundWindow(IntPtr hWnd);
        static void Main()
        {
            
            Process[] processes = Process.GetProcessesByName("notepad");

            foreach (Process proc in processes)
            {
                SetForegroundWindow(proc.MainWindowHandle);
                SendKeys.SendWait("^(s)");
            }
        }
    }
}

添加System.Windows.Forms时要注意添加对应的dll文件

方法:Project-> Add Reference->Browse 

浏览dll所在的文件夹,一般UI自动化的在WPF文件夹中,Forms一般在C:\Windows\Microsoft.NET\Framework\v4.0.30319(根据实际的version)

posted @ 2020-05-28 13:51  strive-sun  阅读(817)  评论(0编辑  收藏  举报