4 using System.Threading;
\r
5 using System.Threading.Tasks;
\r
6 using System.Collections.Generic;
\r
7 using System.Windows.Forms;
\r
8 using System.Diagnostics;
\r
10 using CompleteEraser.Properties;
\r
12 namespace CompleteEraser
\r
14 public partial class MainForm : Form
\r
16 CancellationTokenSource tokenSource;
\r
19 IEnumerable<string> files;
\r
24 InitializeComponent();
\r
27 public MainForm(string[] args) : this()
\r
29 if (args.Length == 0)
\r
33 this.info = new SharedInfo();
\r
34 this.files = this.info;
\r
35 this.op = new ShrredFiles();
\r
39 MessageBox.Show("ファイルかフォルダーを指定してください");
\r
43 else if (args[0] == "/recylebin")
\r
45 this.op = new BreakRecyleBin();
\r
46 this.files = new RecyleBinCollection();
\r
48 this.op = new ShrredFiles();
\r
51 this.progressBar1.Maximum = this.files.Count();
\r
52 this.op.Progressing += new ProgressingEventHandler(op_Progressing);
\r
53 this.op.Progressed += new ProgressedEventHandler(op_Progressed);
\r
56 private void MainForm_Shown(object sender, EventArgs e)
\r
64 this.tokenSource = new CancellationTokenSource();
\r
66 this.task = new Task(() =>
\r
68 op.Execute(this.files);
\r
70 this.task.ContinueWith((t) =>
\r
72 this.BeginInvoke(new Action(() =>
\r
80 void op_Progressing(object sender, ProgressingEventArgs e)
\r
82 this.BeginInvoke(new Action(() =>
\r
84 if (e.fileName == null)
\r
85 this.label2.Text = Resources.FINAL_PROCESS;
\r
87 this.label2.Text = string.Format(Resources.FILE_PROCESS, this.TrimFullPath(e.fileName));
\r
91 void op_Progressed(object sender, ProgressedEventArgs e)
\r
93 CancellationToken token = this.tokenSource.Token;
\r
94 if (token.IsCancellationRequested)
\r
95 token.ThrowIfCancellationRequested();
\r
99 this.BeginInvoke(new Action(() => { if (this.progressBar1.Value < this.progressBar1.Maximum) this.progressBar1.Value++; }));
\r
100 }else if(e.ex is IOException){
\r
101 DialogResult result = MessageBox.Show(e.ex.Message, "", MessageBoxButtons.AbortRetryIgnore);
\r
104 case System.Windows.Forms.DialogResult.Abort:
\r
107 case System.Windows.Forms.DialogResult.Retry:
\r
112 else if (e.ex is UnauthorizedAccessException)
\r
114 if (MessageBox.Show(string.Format(Resources.CONFIRMRUNAS, e.fileName), "", MessageBoxButtons.YesNo) == DialogResult.Yes)
\r
116 ProcessStartInfo info = new ProcessStartInfo(Application.ExecutablePath);
\r
117 info.Verb = "runas";
\r
118 info.UseShellExecute = true;
\r
119 Process.Start(info);
\r
121 Thread.Sleep(1000);
\r
127 MessageBox.Show(e.ex.Message);
\r
132 private void button1_Click(object sender, EventArgs e)
\r
134 tokenSource.Cancel();
\r
137 private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
\r
139 if(tokenSource != null)
\r
140 tokenSource.Cancel();
\r
141 if (this.info != null)
\r
142 this.info.Dispose();
\r
145 public string TrimFullPath(string filepath)
\r
147 if (filepath == null || filepath == "")
\r
148 return string.Empty;
\r
149 string DirectoryPart = Path.GetDirectoryName(filepath);
\r
150 string FilenamePart = Path.GetFileName(filepath);
\r
151 string[] slice = DirectoryPart.Split('\\');
\r
152 if (slice.Length > 3)
\r
154 DirectoryPart = slice[0] + "\\..\\" + slice[slice.Length - 1];
\r
155 return DirectoryPart + "\\" + FilenamePart;
\r