467,104 Members | 1,055 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,104 developers. It's quick & easy.

sendkeys to notepad

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.
Apr 8 '07 #1
  • viewed: 22604
Share:
9 Replies
"Vinki" <Vi***@discussions.microsoft.comwrote in message
news:D3**********************************@microsof t.com...
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.

Apr 8 '07 #2
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);
--
Happy coding!
Morten Wennevik [C# MVP]
Apr 8 '07 #3
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 [C# MVP]" wrote:
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);
--
Happy coding!
Morten Wennevik [C# MVP]
Apr 9 '07 #4
You will not be able to utilize the SendKeys method from a web page.

--
~~~~~~~~~~~
Ben Rush
http://www.ben-rush.net/blog
"Vinki" <Vi***@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
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 [C# MVP]" wrote:
>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);
--
Happy coding!
Morten Wennevik [C# MVP]

Apr 9 '07 #5
On Mon, 09 Apr 2007 02:04:00 +0200, Vinki <Vi***@discussions.microsoft.comwrote:
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.

--
Happy coding!
Morten Wennevik [C# MVP]
Apr 9 '07 #6
Thanks. It worked!!

"Morten Wennevik [C# MVP]" wrote:
On Mon, 09 Apr 2007 02:04:00 +0200, Vinki <Vi***@discussions.microsoft.comwrote:
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.

--
Happy coding!
Morten Wennevik [C# MVP]
Apr 10 '07 #7
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.

"Morten Wennevik [C# MVP]" wrote:
On Mon, 09 Apr 2007 02:04:00 +0200, Vinki <Vi***@discussions.microsoft.comwrote:
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.

--
Happy coding!
Morten Wennevik [C# MVP]
Apr 11 '07 #8
Right click on the icon you click to manually start the exe, check for additional parameters.
Also, try setting working folder.

On Wed, 11 Apr 2007 06:12:02 +0200, Vinki <Vi***@discussions.microsoft.comwrote:
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.

"Morten Wennevik [C# MVP]" wrote:
>On Mon, 09 Apr 2007 02:04:00 +0200, Vinki <Vi***@discussions.microsoft.comwrote:
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.

--
Happy coding!
Morten Wennevik [C# MVP]


--
Happy coding!
Morten Wennevik [C# MVP]
Apr 11 '07 #9
Thanks Morten for all your help. I successfully finished the program.

"Morten Wennevik [C# MVP]" wrote:
Right click on the icon you click to manually start the exe, check for additional parameters.
Also, try setting working folder.

On Wed, 11 Apr 2007 06:12:02 +0200, Vinki <Vi***@discussions.microsoft.comwrote:
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.

"Morten Wennevik [C# MVP]" wrote:
On Mon, 09 Apr 2007 02:04:00 +0200, Vinki <Vi***@discussions.microsoft.comwrote:

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.

--
Happy coding!
Morten Wennevik [C# MVP]

--
Happy coding!
Morten Wennevik [C# MVP]
Apr 13 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bryan | last post: by
4 posts views Thread by Dica | last post: by
6 posts views Thread by Michael Maes | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.