Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to be able to send a keystroke so as to automate opening a program, typing in it and pressing enter. Is there a way to do this? I have a Task Scheduler to wake the computer and run an exe file, but I am not sure how to create the exe file such that it can open an application and do stuff with it automatically (such as enter a username and password to log into an app).

Any and all suggestions would be greatly appreciated!

What I have tried:

I have create a task with Task Scheduler to periodically wake my computer from sleep. I now just need to automate logging in using an app. Thank you!
Posted
Updated 19-Apr-23 19:00pm
Comments
PIEBALDconsult 19-Apr-23 21:43pm    
Seek ye the Process and ProcessStart classes (in System.Diagnostics I think).
You will have to redirect the standard input and probably standard output to interact with the process.

Much of what you want can be found in -- https://www.codeproject.com/Articles/70864/ProcessCommunicator

1 solution

Opening an app is easy:
C#
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"Path to file";
psi.Arguments = $"List of arguments to start it with";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
p.StartInfo = psi;
p.Start();
And sending data to it is also simple:
C#
StreamWriter sw = p.StandardInput;
sw.WriteLine("username");
sw.WriteLine("password");
sw.Close();
}
 
Share this answer
 
Comments
Pada Temp 20-Apr-23 5:37am    
This is super useful. Thank ye :)

As this is an app wih graphical gui with a login and password, would there be arguments to start it with in the line
psi.Arguments = $"List of arguments to start it with"; ?

I also want the C# code to return control to itself after performing actions. the sw.Close() alone does this or would I also maybe add psi.RedirectStandardInput = false; and psi.RedirectStandardOutput = false; ?
PIEBALDconsult 20-Apr-23 8:57am    
But you stated that it was a console app in the question!
Automating a GUI app is a whole different matter.
Pada Temp 20-Apr-23 10:26am    
ah sorry what i meant is that C# code from which this is occuring is a console app. i want to have the console app open a GUI app, which now I can do with OriginalGriff's code. then i want the console app to at, the minimum, simulate pressing enter key, so as to have the GUI app log in with pre-populated info on the splash screen....ideally, i would want the console app to do more complex stuff with the GUI app, such as perhaps enter a username and password, prior to simulating a keystroke enter, and then maybe do stuff in the GUI app that a user might do.
PIEBALDconsult 20-Apr-23 10:35am    
I don't have any examples handy (at the office), but what simple examples I have use the Process class to find the GUI application window, then use API calls to find a specific button (such as the OK button), and then click it. Maybe someone else will respond, otherwise I'll have a look when I get home.
Pada Temp 20-Apr-23 11:28am    
would be most appreciated if you remember to get around to it. you have been already so helpful and i have a good point of reference for further research! cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900