using System; using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; using System.Windows.Forms; using System.Diagnostics; using System.Linq; using CompleteEraser.Properties; namespace CompleteEraser { public partial class MainForm : Form { CancellationTokenSource tokenSource; Task task; SharedInfo info; IEnumerable files; IOperation op; public MainForm() { InitializeComponent(); } public MainForm(string[] args) : this() { if (args.Length == 0) { try { this.info = new SharedInfo(); this.files = this.info; this.op = new ShrredFiles(); } catch (IOException) { MessageBox.Show("ファイルかフォルダーを指定してください"); return; } } else if (args[0] == "/recylebin") { this.op = new BreakRecyleBin(); this.files = new RecyleBinCollection(); }else{ this.op = new ShrredFiles(); this.files = args; } this.progressBar1.Maximum = this.files.Count(); this.op.Progressing += new ProgressingEventHandler(op_Progressing); this.op.Progressed += new ProgressedEventHandler(op_Progressed); this.op.Complete += op_Complete; } void op_Complete(object sender, EventArgs e) { this.Close(); } private void MainForm_Shown(object sender, EventArgs e) { if (op == null) { this.Close(); return; } this.tokenSource = new CancellationTokenSource(); op.ExecuteAsync(this.files,this.tokenSource.Token); } void op_Progressing(object sender, ProgressingEventArgs e) { if (e.fileName == null) this.label2.Text = Resources.FINAL_PROCESS; else this.label2.Text = string.Format(Resources.FILE_PROCESS, Path.GetFileName(e.fileName)); } void op_Progressed(object sender, ProgressedEventArgs e) { if (e.ex == null) { if (this.progressBar1.Value < this.progressBar1.Maximum) this.progressBar1.Value++; }else if(e.ex is IOException){ DialogResult result = MessageBox.Show(e.ex.Message, "", MessageBoxButtons.AbortRetryIgnore); switch (result) { case System.Windows.Forms.DialogResult.Abort: e.breaked = true; break; case System.Windows.Forms.DialogResult.Retry: e.retry = true; break; } } else if (e.ex is UnauthorizedAccessException) { if (MessageBox.Show(string.Format(Resources.CONFIRMRUNAS, e.fileName), "", MessageBoxButtons.YesNo) == DialogResult.Yes) { ProcessStartInfo info = new ProcessStartInfo(Application.ExecutablePath); info.Verb = "runas"; info.UseShellExecute = true; if (this.info == null) info.Arguments = string.Join(" ", this.files); Process.Start(info); this.Hide(); Thread.Sleep(1000); e.breaked = true; } } else { MessageBox.Show(e.ex.Message); e.breaked = true; } } private void button1_Click(object sender, EventArgs e) { tokenSource.Cancel(); } private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { if(tokenSource != null) tokenSource.Cancel(); if (this.info != null) this.info.Dispose(); } } }