ninjatrader/utilities/NinjaTraderLauncher/Program.cs

54 lines
1.8 KiB
C#

using DotNetEnv;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace NinjaTraderLauncher
{
class Program
{
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[STAThread]
static void Main(string[] args)
{
string ninjaTraderPath = @"C:\Program Files\NinjaTrader 8\bin\NinjaTrader.exe";
Process ninjaTraderProcess = new Process();
ninjaTraderProcess.StartInfo.FileName = ninjaTraderPath;
ninjaTraderProcess.Start();
// Wait until NinjaTrader is running
while (true)
{
Thread.Sleep(1000);
ninjaTraderProcess.Refresh();
if (!ninjaTraderProcess.HasExited)
break;
}
// Bring the login window to the foreground
SetForegroundWindow(ninjaTraderProcess.MainWindowHandle);
Thread.Sleep(1000); // Allow time for the window to come to the foreground
// Load the password from environment variables
Env.Load();
string password = Env.GetString("NINJATRADER_PASSWORD");
// Update the clipboard with the password
Clipboard.SetText(password);
Console.WriteLine("NinjaTrader launched and password copied to clipboard.");
// It takes three tabs to get to the password field once the window is in focus.
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("{TAB}");
// Paste the password (Ctrl+v) and press enter to login
SendKeys.SendWait("^(v)");
SendKeys.SendWait("{ENTER}");
Clipboard.Clear();
}
}
}