Sending data to a notepad using processes

后端 未结 5 1248
余生分开走
余生分开走 2021-01-23 21:11

I want to send every item from my listbox to a notepad,but my logic is kinda beating me.

private void send_Click(object sender, EventArgs e)
{
    var notepad =          


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-23 22:13

    From @GillBates suggestion I used System.Threading.Thread.Sleep(); and it seem to work great now.

    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);
                    }
    
                }
    }
    

提交回复
热议问题