1
0
mirror of https://github.com/xCryptic/MegaKeep.git synced 2024-11-22 01:52:30 +01:00

Added ability to run via command line with arguments "--cli" and can set file path with "--txtFile" #5 (#6)

This commit is contained in:
CrazyOldWizard 2021-01-18 20:56:51 -06:00 committed by GitHub
parent 10a4d09007
commit b76965bf63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 3 deletions

View File

@ -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;
}

View File

@ -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.
/// </summary>
[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();
}
}
}
}
}
}