C# 使用进程将数据发送到记事本

C# 使用进程将数据发送到记事本,c#,winforms,C#,Winforms,我想把列表框中的每一项都发送到记事本上,但我的逻辑有点让我不知所措 private void send_Click(object sender, EventArgs e) { var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad"); if (notepad != null) {

我想把列表框中的每一项都发送到记事本上,但我的逻辑有点让我不知所措

private void send_Click(object sender, EventArgs e)
{
    var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad");

    if (notepad != null)
    {
        if (IsIconic(notepad.MainWindowHandle))
            ShowWindow(notepad.MainWindowHandle, 9);

        SetForegroundWindow(notepad.MainWindowHandle);
        string text = "";

        foreach (var item in listBox1.Items)
        {
            text = item.ToString();
            Clipboard.SetText(text);
            SendKeys.Send("^V");
            SendKeys.Send("{ENTER}");
        }
    }
}

按照我的逻辑,这应该将列表框中的每一项都发送到记事本中不同行中的每一项。但并非每次都是这样,有时它只发送列表框中最后一项,与列表框中的项目一样多。我遗漏了什么吗?

根据@GillBates的建议,我使用了
System.Threading.Thread.Sleep()
现在似乎工作得很好

private void send_Click(object sender, EventArgs e)
{
 var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad");

            if (notepad != null)
            {
                if (IsIconic(notepad.MainWindowHandle))
                    ShowWindow(notepad.MainWindowHandle, 9);

                SetForegroundWindow(notepad.MainWindowHandle);
                string text = "";

                foreach (var item in listBox1.Items)
                {
                    text = item.ToString();
                    Clipboard.SetText(text);
                    SendKeys.Send("^V");
                    SendKeys.Send("{ENTER}");
                    System.Threading.Thread.Sleep(150);
                }

            }
}

根据@GillBates的建议,我使用了
System.Threading.Thread.Sleep()
现在似乎工作得很好

private void send_Click(object sender, EventArgs e)
{
 var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad");

            if (notepad != null)
            {
                if (IsIconic(notepad.MainWindowHandle))
                    ShowWindow(notepad.MainWindowHandle, 9);

                SetForegroundWindow(notepad.MainWindowHandle);
                string text = "";

                foreach (var item in listBox1.Items)
                {
                    text = item.ToString();
                    Clipboard.SetText(text);
                    SendKeys.Send("^V");
                    SendKeys.Send("{ENTER}");
                    System.Threading.Thread.Sleep(150);
                }

            }
}

我有一个示例测试,如果您更改SendKeys,它会工作。发送到SendKeys.SendWait

    List<string> data = new List<string>() { "Test", "hope", "It", "works","Or" };
    foreach (var item in data)
    {
        Clipboard.Clear();
        Clipboard.SetText(item);
        //SendKeys.Send("^V");
        //SendKeys.Send("{ENTER}");

        SendKeys.SendWait("^V");
        SendKeys.SendWait("{ENTER}");
    }
List data=newlist(){“Test”、“hope”、“It”、“works”或“};
foreach(数据中的var项)
{
剪贴板。清除();
剪贴板.SetText(项目);
//SendKeys.Send(“^V”);
//SendKeys.Send(“{ENTER}”);
SendKeys.SendWait(“^V”);
SendKeys.SendWait(“{ENTER}”);
}

由于应用的密钥晚于更新剪贴板和循环,因此问题发生。

我有一个示例测试,如果您更改SendKeys,它会起作用。发送到SendKeys.SendWait

    List<string> data = new List<string>() { "Test", "hope", "It", "works","Or" };
    foreach (var item in data)
    {
        Clipboard.Clear();
        Clipboard.SetText(item);
        //SendKeys.Send("^V");
        //SendKeys.Send("{ENTER}");

        SendKeys.SendWait("^V");
        SendKeys.SendWait("{ENTER}");
    }
List data=newlist(){“Test”、“hope”、“It”、“works”或“};
foreach(数据中的var项)
{
剪贴板。清除();
剪贴板.SetText(项目);
//SendKeys.Send(“^V”);
//SendKeys.Send(“{ENTER}”);
SendKeys.SendWait(“^V”);
SendKeys.SendWait(“{ENTER}”);
}

因为密钥应用的时间晚于更新剪贴板和循环,因此问题会发生。

您可以使用InputSimulator、软件包管理控制台: 安装软件包输入模拟器

 private void Button_Click_1(object sender, RoutedEventArgs e)
            {

                var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad");
                if (notepad != null)
                {
                    if (IsIconic(notepad.MainWindowHandle))
                        ShowWindow(notepad.MainWindowHandle, 9);

                    var input = new InputSimulator();
                    SetForegroundWindow(notepad.MainWindowHandle);
                    foreach (var item in listBox1.Items)
                    {
                        input.Keyboard.TextEntry(item.ToString());
                        input.Keyboard.KeyPress(VirtualKeyCode.RETURN);

                    }
                }
            }

您可以使用InputSimulator、软件包管理控制台: 安装软件包输入模拟器

 private void Button_Click_1(object sender, RoutedEventArgs e)
            {

                var notepad = Process.GetProcessesByName("Notepad").FirstOrDefault(p => p.MainWindowTitle == "Untitled - Notepad");
                if (notepad != null)
                {
                    if (IsIconic(notepad.MainWindowHandle))
                        ShowWindow(notepad.MainWindowHandle, 9);

                    var input = new InputSimulator();
                    SetForegroundWindow(notepad.MainWindowHandle);
                    foreach (var item in listBox1.Items)
                    {
                        input.Keyboard.TextEntry(item.ToString());
                        input.Keyboard.KeyPress(VirtualKeyCode.RETURN);

                    }
                }
            }

您可以使用另一个选项找到记事本控件,并使用向其发送消息。这样,您就不需要将记事本窗口放在前面

using System.Diagnostics;
using System.Runtime.InteropServices;

您可以使用另一个选项找到记事本控件,并使用向其发送消息。这样,您就不需要将记事本窗口放在前面

using System.Diagnostics;
using System.Runtime.InteropServices;


比使用
System.Threading.Thread.Sleep()更好使用
等待任务。延迟()

比使用
System.Threading.Thread.Sleep()更好使用
等待任务。延迟()

尝试放慢粘贴部分的速度,可能是一个睡眠,我觉得你粘贴得太快了。没有考虑到这一点,非常好的提示:)与其发送单个项目,不如使用
String.Join
将所有项目组合在一个字符串中,例如
String.Join(“\n”,listBox1.items)
如果我将它们合并在一个字符串中,如何将它们发送到一行?@JohnPietrar,在每个项目后添加新行
\n
。尝试放慢粘贴部分的速度,可能是一个睡眠,我觉得你试图粘贴得太快。没有想到这一点,非常好的提示:)而不是发送单个项目,只需使用
String.Join
将它们组合成一个字符串,例如
String.Join(“\n”,listBox1.Items)
如果我将它们合并在一个字符串中,如何将它们发送到一行?@JohnPietrar,在每个项目后添加新行
\n
。不知道
发送键。SendWait
。感谢您与我共享:)这似乎不起作用,在大约40次尝试之后,我又不幸地犯了这个错误。我不知道
SendKeys.SendWait
。谢谢你与我分享这个:)似乎这不起作用,在大约40次尝试之后,我又不幸地犯了这个错误。这到底是怎么回事?你能解释得更详细些吗?:)因为我从来没有听说过InputSimulatorits是一个用于向应用程序发送密钥的包装器。他们在其中管理了完成处理机制的代码。@JohnPietrar您可以参考这个,这到底是做什么的?您能详细解释一下吗?:)因为我从未听说过InputSimulatorits是一个用于向应用程序发送密钥的包装器。他们在其中管理了完成处理机制的代码。@JohnPietrar您可以参考此解决方案。此解决方案比使用sendkeys发送字符串或粘贴字符串更优雅。如果将记事本应用程序最小化,它也可以工作。如果您对答案有任何疑问,请告诉我:)我请求您帮助,因为我知道您可以一步一步地给出正确的解释,不仅要回答它,也许我可以一劳永逸地关闭这个
winhandle
主题。我现在想到的想法是:你可以在计时器
事件中使用
EnumChildWindows
,并检查子窗口的标题是否是
页面设置
(可能是,它的类名是
\32770(Dialog)
)然后做你想做的工作。我现在有更多的问题,哈哈。。。如果我问得好的话,你能在那里发布一个答案吗?这样我就能更好地理解了。我为你发布了答案。这个解决方案比使用sendkeys发送字符串或粘贴字符串更优雅。如果将记事本应用程序最小化,它也可以工作。如果您对答案有任何疑问,请告诉我:)我请求您帮助,因为我知道您可以一步一步地给出正确的解释,不仅要回答它,也许我可以一劳永逸地关闭这个
winhandle
主题。我现在想到的想法是:你可以在计时器
事件中使用
EnumChildWindows
,并检查子窗口的标题是否是
页面设置
(可能是,它的类名是
\32770(Dialog)
)然后做你想做的工作。我现在有更多的问题,哈哈。。。如果我问得好的话,你能把答案贴在那里吗?这样我就能更好地理解了。我帮你贴了答案。