From b76965bf630765a6d6bc65a10cb095ce458fddda Mon Sep 17 00:00:00 2001 From: CrazyOldWizard Date: Mon, 18 Jan 2021 20:56:51 -0600 Subject: [PATCH] Added ability to run via command line with arguments "--cli" and can set file path with "--txtFile" #5 (#6) --- MegaKeep/MegaKeep.cs | 26 +++++++++++++++++++++++++- MegaKeep/Program.cs | 25 +++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/MegaKeep/MegaKeep.cs b/MegaKeep/MegaKeep.cs index 73afd80..3c8151c 100644 --- a/MegaKeep/MegaKeep.cs +++ b/MegaKeep/MegaKeep.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -10,11 +11,27 @@ namespace MegaKeep public partial class MegaKeep : Form { private string _local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); - + private string[] commandlineArgs = { }; public MegaKeep() { InitializeComponent(); } + public MegaKeep(string[] args) + { + InitializeComponent(); + if (args.Length > 0) + { + commandlineArgs = args; + var txtFileIndex = Array.IndexOf(args, "--txtFile") + 1; + var txtFile = args[txtFileIndex]; + if (File.Exists(txtFile)) + { + Properties.Settings.Default.Location = txtFile; + Properties.Settings.Default.Save(); + txtPath.Text = txtFile; + } + } + } private async void btnRun_ClickAsync(object sender, EventArgs e) { @@ -50,6 +67,8 @@ namespace MegaKeep // run the processes in a task so it doesn't freeze the ui await Task.Run(() => Work(lines)); + if (commandlineArgs.Contains("--cli"))// if running in cli mode, closes window after running work. + this.Close(); } private string Login(string user, string pass) @@ -225,6 +244,11 @@ namespace MegaKeep private void MegaKeep_Load(object sender, EventArgs e) { + // if started with arg "--cli" simulate button click in UI + if (commandlineArgs.Contains("--cli")) + { + btnRun.PerformClick(); + } txtPath.Text = Properties.Settings.Default.Location; } diff --git a/MegaKeep/Program.cs b/MegaKeep/Program.cs index 6132539..dfdf65c 100644 --- a/MegaKeep/Program.cs +++ b/MegaKeep/Program.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Windows.Forms; namespace MegaKeep @@ -9,11 +10,31 @@ namespace MegaKeep /// The main entry point for the application. /// [STAThread] - static void Main() + static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MegaKeep()); + CommandArgs(args); + Application.Run(new MegaKeep(args)); //passes args to new start method + } + + // sets the path to the text file passed via command line. + static void CommandArgs(string[] args) + { + if (args.Length > 0) + { + var txtFileIndex = Array.IndexOf(args, "--txtFile") + 1; + if(args[txtFileIndex] != null) + { + var txtFile = args[txtFileIndex]; + if (File.Exists(txtFile)) + { + Properties.Settings.Default.Location = txtFile; + Properties.Settings.Default.Save(); + } + } + + } } } }