diff --git a/README.md b/README.md index 9ad344b..d436c5a 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ SendKeys has no dependencies besides the .NET Framework 2.0 and can be deployed ###### Same but wait 3 seconds upfront: `SendKeys.exe -pid:4711 "format C:{Enter}" -wait:3000` +###### Same but read a file and send the contents of that file to the new pid: +`SendKeys.exe -pid:4711 -wait:3000 -file:"C:\test.txt"` + + ## Noteworthy As always, you'll need to add quotes to the argument string if it contains spaces (like shown in the examples). Otherwise, Windows will split it up as multiple arguments. diff --git a/SendKeys/Program.cs b/SendKeys/Program.cs index 74d9690..ae90728 100644 --- a/SendKeys/Program.cs +++ b/SendKeys/Program.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; +using System.IO; namespace SendKeys { @@ -26,9 +27,13 @@ static void Main(string[] args) { int pid = -1; int wait = 0; + int file = 0; + string line; + bool filepresent = false; + string filetoread = ""; string keysToSend = ""; - var validArguments = args?.Length == 2 || args?.Length == 3; + var validArguments = args?.Length == 2 || args?.Length == 3 || args?.Length == 4; if (validArguments) { @@ -36,6 +41,7 @@ static void Main(string[] args) { int pidIndex = args[i].IndexOf("pid:", StringComparison.OrdinalIgnoreCase); int waitIndex = args[i].IndexOf("wait:", StringComparison.OrdinalIgnoreCase); + int fileIndex = args[i].IndexOf("file:", StringComparison.OrdinalIgnoreCase); if (pidIndex > -1) { var pidString = args[i].Substring(pidIndex + "pid:".Length); @@ -46,6 +52,13 @@ static void Main(string[] args) var waitString = args[i].Substring(waitIndex + "wait:".Length); int.TryParse(waitString, out wait); } + else if (fileIndex > -1) + { + var fileString = args[i].Substring(waitIndex + "file:".Length + 2); + int.TryParse(fileString, out file); + filepresent = true; + filetoread = fileString; + } else { keysToSend = args[i].Replace("'", "\""); @@ -57,12 +70,13 @@ static void Main(string[] args) { WriteError("Invalid arguments. Please define a process id and the string value to send as keys." + "\n Example: SendKeys.exe -pid:4711 \"Keys to send{Enter}\"" + - "\n Optional: Add -wait:100 to add a delay of 100 milliseconds, for example."); + "\n Optional: Add -wait:100 to add a delay of 100 milliseconds, for example." + + "\n Optional: Add -file:'test.txt' to read the contents of a file."); return; } - Process process = null ; + Process process = null; try { process = Process.GetProcessById(pid); @@ -81,9 +95,38 @@ static void Main(string[] args) { if (wait > 0) Thread.Sleep(wait); + if (filepresent) + { + SetForegroundWindow(process.MainWindowHandle); + try + { + StreamReader sr = new StreamReader(filetoread); + int linenumber = 0; + line = sr.ReadLine(); + while (sr.Peek() > -1) + { + SetForegroundWindow(process.MainWindowHandle); + string keytosendline = line.Replace("+", "{+}"); + System.Windows.Forms.SendKeys.SendWait(keytosendline + '\n'); + WriteInfo("On line: " + linenumber.ToString()); + + linenumber = linenumber + 1; + line = sr.ReadLine(); + } + sr.Close(); + } + catch (Exception e) + { + WriteError("Exception: " + e.Message); + + } - SetForegroundWindow(process.MainWindowHandle); - System.Windows.Forms.SendKeys.SendWait(keysToSend); + } + else + { + SetForegroundWindow(process.MainWindowHandle); + System.Windows.Forms.SendKeys.SendWait(keysToSend); + } } } @@ -93,5 +136,12 @@ private static void WriteError(string message) Console.WriteLine(message); FreeConsole(); } + + public static void WriteInfo(string message) + { + AttachConsole(ATTACH_PARENT_PROCESS); + Console.WriteLine(message); + FreeConsole(); + } } } diff --git a/SendKeys/Properties/Resources.Designer.cs b/SendKeys/Properties/Resources.Designer.cs index c80c94c..21a88ec 100644 --- a/SendKeys/Properties/Resources.Designer.cs +++ b/SendKeys/Properties/Resources.Designer.cs @@ -8,64 +8,56 @@ // //------------------------------------------------------------------------------ -namespace SendKeys.Properties -{ - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources - { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() - { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager - { - get - { - if ((resourceMan == null)) - { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SendKeys.Properties.Resources", typeof(Resources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture - { - get - { - return resourceCulture; - } - set - { - resourceCulture = value; - } - } - } +namespace SendKeys.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("SendKeys.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } } diff --git a/SendKeys/Properties/Settings.Designer.cs b/SendKeys/Properties/Settings.Designer.cs index 937a197..b865763 100644 --- a/SendKeys/Properties/Settings.Designer.cs +++ b/SendKeys/Properties/Settings.Designer.cs @@ -8,23 +8,19 @@ // //------------------------------------------------------------------------------ -namespace SendKeys.Properties -{ - - - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase - { - - private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default - { - get - { - return defaultInstance; - } - } - } +namespace SendKeys.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.4.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + } } diff --git a/SendKeys/SendKeys.csproj b/SendKeys/SendKeys.csproj index dba03aa..20c4a1c 100644 --- a/SendKeys/SendKeys.csproj +++ b/SendKeys/SendKeys.csproj @@ -8,9 +8,10 @@ WinExe SendKeys SendKeys - v2.0 + v4.6.1 512 true + AnyCPU @@ -21,6 +22,7 @@ DEBUG;TRACE prompt 4 + false AnyCPU @@ -30,6 +32,7 @@ TRACE prompt 4 + false SendKeys.Program @@ -56,7 +59,9 @@ True Resources.resx + True + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/SendKeys/app.config b/SendKeys/app.config new file mode 100644 index 0000000..3dbff35 --- /dev/null +++ b/SendKeys/app.config @@ -0,0 +1,3 @@ + + +