sendkeys to notepad

G

Guest

Hello Everyone,

I have this code for sendKeys. This simply sends a text to the notepad.
This method runs fine, but I don't see the notepad and the text entered in
that notepad. Is there any way I can see the notepad. When I go to the
taskbar, I can see that notepad.exe is running there, but I don't see the
notepad itself. I am calling this method from the web page.

public void sendKeysTest()
{
System.Diagnostics.Process.Start("c:\WINNT\notepad.exe")

Process myProcess= new Process();
myProcess.StartInfo.FileName="notepad";
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
myProcess.EnableRaisingEvents = true;
if (myProcess.Responding)
{
System.Windows.Forms.SendKeys.SendWait("This text was entered using the
System.Windows.Forms.SendKeys method.");

}
else
myProcess.Kill();




}
Thanks,

Any help will be appreciated.
 
A

Alberto Poblacion

Vinki said:
I have this code for sendKeys. This simply sends a text to the notepad.
This method runs fine, but I don't see the notepad and the text entered in
that notepad. Is there any way I can see the notepad. When I go to the
taskbar, I can see that notepad.exe is running there, but I don't see the
notepad itself. I am calling this method from the web page.

If you are caling it from the *server code* in a web page, it is
starting Notepad on the server, not on the computer where you are running
the browser. Even if you are running the browser on the same computer as the
web server, the IIS server runs as a service that by default is not allowed
to interact with the desktop, so a program launched from server code will
not appear on screen.
 
M

Morten Wennevik [C# MVP]

Hi Vinki,

The problem is, Notepad isn't the active window when you use SendKeys, so you effectively send the text to your own application instead.

To bring notepad to the front tap into the win32 api and use the SetForegroundWindow method

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public void sendKeysTest()
{
Process myProcess = Process.Start(@"k:\windows\notepad.exe");

SetForegroundWindow(myProcess.Handle);

if (myProcess.Responding)
SendKeys.SendWait("This text was entered using the System.Windows.Forms.SendKeys method.");
else
myProcess.Kill();
}

// This means use the SetForegroundWindow method defined in user32.dll
// If you read the documentation for SetForegroundWindow, you will see the
// parameters defined as HWND hWnd. A HWND is equivalent to IntPtr in C#
// and since Process has a Handle property, we are all set

[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
 
G

Guest

Thanks, I will try this code and will let you know. I have one question
though. Will this code work fine in asp.net because I am using asp.net
application not windows application. Everyone told me that I can use sendKeys
only in windows application.

Thanks.
 
B

Ben Rush

You will not be able to utilize the SendKeys method from a web page.

--
~~~~~~~~~~~
Ben Rush
http://www.ben-rush.net/blog


Vinki said:
Thanks, I will try this code and will let you know. I have one question
though. Will this code work fine in asp.net because I am using asp.net
application not windows application. Everyone told me that I can use
sendKeys
only in windows application.

Thanks.

Morten Wennevik said:
Hi Vinki,

The problem is, Notepad isn't the active window when you use SendKeys, so
you effectively send the text to your own application instead.

To bring notepad to the front tap into the win32 api and use the
SetForegroundWindow method

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public void sendKeysTest()
{
Process myProcess = Process.Start(@"k:\windows\notepad.exe");

SetForegroundWindow(myProcess.Handle);

if (myProcess.Responding)
SendKeys.SendWait("This text was entered using the
System.Windows.Forms.SendKeys method.");
else
myProcess.Kill();
}

// This means use the SetForegroundWindow method defined in user32.dll
// If you read the documentation for SetForegroundWindow, you will see
the
// parameters defined as HWND hWnd. A HWND is equivalent to IntPtr in C#
// and since Process has a Handle property, we are all set

[DllImport("User32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
 
M

Morten Wennevik [C# MVP]

Thanks, I will try this code and will let you know. I have one question
though. Will this code work fine in asp.net because I am using asp.net
application not windows application. Everyone told me that I can use sendKeys
only in windows application.

Thanks.

Actually, it will work in a web application, but it would only open Notepad on the web server.
 
G

Guest

I was wondering that the behaviour of exe file changes if I execute the file
by sendkeys or manually.

I am trying to execute this exe file through sendkeys and my .net
application opens the exe file, but says that exe file license has expired,
but when I manually click on the exe file, the exe application opens. I am
not sure why this happening.
 
M

Morten Wennevik [C# MVP]

Right click on the icon you click to manually start the exe, check for additional parameters.
Also, try setting working folder.

I was wondering that the behaviour of exe file changes if I execute the file
by sendkeys or manually.

I am trying to execute this exe file through sendkeys and my .net
application opens the exe file, but says that exe file license has expired,
but when I manually click on the exe file, the exe application opens. I am
not sure why this happening.
 
G

Guest

Thanks Morten for all your help. I successfully finished the program.

Morten Wennevik said:
Right click on the icon you click to manually start the exe, check for additional parameters.
Also, try setting working folder.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top