2

I thought that code is pretty good, bu it isn't. I'm trying to add something text to my notepad, which look like:

string text = "TESTTESTTESTTEST";

[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);

public void EditTxtFile(string text)
{
    Process p = Process.GetProcessesByName("notepad").FirstOrDefault();
    if (p != null)
    {
        IntPtr handle = p.MainWindowHandle;
        SetForegroundWindow(handle);
        SendKeys.SendWait(text);
        p.Kill(); //also process doesn't shoutdown
    }
}

When i try to debug this function (actually SendKeys), that message is showing up:

Changes are not allowed while code is runnig.

If it's important i try to edit this from web page/application.

p__d
  • 443
  • 2
  • 7
  • 19
  • 4
    That error is when you trying and edit your C# code while debugging - it's enough to just press 'a' on the keyboard while the editor is focused. It has nothing to do with the code itself... Be more careful with your fingers :) – Rob Sep 10 '15 at 12:34
  • 2
    Actually, @Rob, that edit is probably when `SendKeys` are being sent to the editor window whilst debugging. – MicroVirus Sep 10 '15 at 12:34
  • It works for me, although p.Kill() closes notepad, making the entire exercise redundant – Callum Bradbury Sep 10 '15 at 12:34
  • I have never played with that kind of scripts, but I wonder if the debugging + SetForegroundWindow isn't the problem. With SetForegroundWindow, you get the notepad in the front, but with the debugging, you get your IDE back in the front. The SendKeys might thus be sent to the IDE, not your notepad... – Timothée Bourguignon Sep 10 '15 at 12:36
  • @MicroVirus Ah, yes - you're probably right.. Which means one of the following: 1. `SetForegroundWindow(handle);` isn't working. 2. `SetForegroundWindow(handle);` is working, but VS is immediately being focused again or 3. `SetForegroundWindow(handle);` needs a few cycles to process before `SendKeys.SendWait()` is executed – Rob Sep 10 '15 at 12:37
  • @TIMbourguignon when I run test without debuging nothing is changing in my notepad. – p__d Sep 10 '15 at 12:39
  • In this [thread](http://stackoverflow.com/questions/10286086/sendkeys-sendwait-does-not-send-enter-in-windows-7), the answer mentions a problem with administrator rights. Have you tried this? – Timothée Bourguignon Sep 10 '15 at 12:43

3 Answers3

2

The problem is, that Visual Studio catches Focus when it hits the break point, and then SendKeys is send to Visual Studio instead of notepad, and this produces the error.

What you can do ist the following:

Right Click on the Breakpoint and select "When Hit..."

There you can output whatever you want without Notepad losing Focus

Bgl86
  • 727
  • 8
  • 20
1

Problem was in function which was responsible for opening that notepad (not enough time to start and open him). I've just add wait function for 4s.

p__d
  • 443
  • 2
  • 7
  • 19
0

To mine, it's about I use a reference value text from the source WinForms's control, call SendKeys' function while the source form is not yet close or hide before (still the active form). This can produce a similar result: SendKeys.Send() or SendKeys.SendWait() does not send a desired text to the target application.

The steps to workaround this:

1. transfer the text value from the active form to a new temporary variation or a Clipboard,

2. close or hide the active form,

3. activate the target application's form,

4. wait a bit to ensure the target application's form to become active

5. and give the temporary text variation or Clipboard to SendKeys. Works.

PS: Please make sure your application has runtime permission equals or more than the target application.