using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Text; using System.Windows.Forms; using System.Linq; namespace AutoPekage { public partial class Form1 : Form { public static string tempPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.Combine("AutoPackage", "tempfile")); //Dictionary public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { var dao = LiteDbHelper.Settings; if (Directory.Exists(tempPath) && ((dao.AskAutoLoadFile && MessageBox.Show("发现临时目录存在文件,是否自动加载内容?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) || (!dao.AskAutoLoadFile && dao.AutoLoadFile == 1))) { var files = Directory.GetFiles(tempPath); foreach (var file in files) { AddNewFile(file); } } } private void Form1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Link; } else { e.Effect = DragDropEffects.None; } } private void Form1_DragDrop(object sender, DragEventArgs e) { var dropList = (System.Array)e.Data.GetData(DataFormats.FileDrop); foreach (var item in dropList) { string filePath = item.ToString(); AddDropFile(filePath); } } private void AddDropFile(string filePath) { FileInfo fi = new FileInfo(filePath); if (fi.Attributes == FileAttributes.Directory) { var files = Directory.EnumerateFiles(filePath, "*", SearchOption.AllDirectories); foreach (var file in files) { AddDropFile(file); } return; } string extension = Path.GetExtension(filePath); if (extension.ToLower() == ".zip" || extension.ToLower() == ".rar" || extension.ToLower() == ".7z" || extension.ToLower() == ".gzip" || extension.ToLower() == ".tar") { ZipControl.NewFileDecompress += AddNewFile; //ZipControl.UnZip(filePath, ""); ZipControl.FileWillDecompress += ZipControl_FileWillDecompress; ZipControl.UnCompress(filePath, tempPath); ZipControl.FileWillDecompress -= ZipControl_FileWillDecompress; ZipControl.NewFileDecompress -= AddNewFile; } else { var itemlist = lvFileList.Items.Cast().Where(x => Path.GetFileName(x.Tag as string) == Path.GetFileName(filePath)); if (itemlist.Count() > 0) { if (itemlist.First().Tag as string == filePath) { return; } else { if (MessageBox.Show(Path.GetFileName(filePath) + "已存在并且与目前列表中文件不同,是否替换?" , "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { AddNewFile(filePath); } } } else { AddNewFile(filePath); } } } private void AddNewFile(string filePath) { var itemlist = lvFileList.Items.Cast().Where(x => Path.GetFileName(x.Tag as string) == Path.GetFileName(filePath)); if (itemlist.Count() > 0) { itemlist.First().Tag = filePath; } else { ilFileList.Images.Add(GetIcon.GetFileIcon(filePath)); string filename = Path.GetFileName(filePath); var item = new ListViewItem(filename); item.ImageIndex = ilFileList.Images.Count - 1; item.Tag = filePath; lvFileList.Items.Add(item); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { var dao = LiteDbHelper.Settings; if (Directory.Exists(tempPath) && ((dao.AskAutoDeleteFile && MessageBox.Show("发现临时目录存在文件,是否删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)|| (!dao.AskAutoDeleteFile && dao.AutoDeleteFile == 1))) { Directory.Delete(tempPath, true); } } private void cmsFileContextMenu_Opening(object sender, CancelEventArgs e) { bool showMenu = false; //要判断是否点击项,控件里至少要有一个项 if (this.lvFileList.Items.Count > 0) { Point point = this.lvFileList.PointToClient(MousePosition); ListViewHitTestInfo hitTest = this.lvFileList.HitTest(point); if ( hitTest.Item != null) {//点击的不是列且不是空白区域 showMenu = true; } } e.Cancel = !showMenu; } private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e) { foreach (ListViewItem item in lvFileList.SelectedItems) { Process.Start(item.Tag as string); } } private void 移除文件ToolStripMenuItem_Click(object sender, EventArgs e) { foreach (ListViewItem item in lvFileList.SelectedItems) { lvFileList.Items.Remove(item); } } private void 打开所在目录ToolStripMenuItem_Click(object sender, EventArgs e) { foreach (ListViewItem item in lvFileList.SelectedItems) { Process.Start("Explorer.exe", Path.GetDirectoryName(item.Tag as string)); } } private void lvFileList_ItemCheck(object sender, ItemCheckEventArgs e) { } private void lvFileList_DoubleClick(object sender, EventArgs e) { //要判断是否点击项,控件里至少要有一个项 if (this.lvFileList.Items.Count > 0) { Point point = this.lvFileList.PointToClient(MousePosition); ListViewHitTestInfo hitTest = this.lvFileList.HitTest(point); if (hitTest.Item != null) {//点击的不是列且不是空白区域 Process.Start(lvFileList.SelectedItems[0].Tag as string); } } } private void btnStart_Click(object sender, EventArgs e) { if (lvFileList.Items.Count == 0) { MessageBox.Show("打包前至少应该有一个文件", "自动打包", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } frmPackage package = new frmPackage(lvFileList.Items.Cast().Select(x => x.Tag as string).ToList()); package.ShowDialog(); } bool ZipControl_FileWillDecompress(string filePath) { if (lvFileList.Items.Cast().Where(x => Path.GetFileName(x.Tag as string) == Path.GetFileName(filePath)).Count() > 0) { if (MessageBox.Show(Path.GetFileName(filePath) + "已存在在列表中,是否覆盖?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { return true; } else { return false; } } return true; } private void btnExit_Click(object sender, EventArgs e) { Close(); } private void btnSettings_Click(object sender, EventArgs e) { new frmSettings().ShowDialog(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { //要判断是否点击项,控件里至少要有一个项 if (this.lvFileList.SelectedItems.Count > 0) { if(e.Shift && e.KeyCode == Keys.Delete) { if (MessageBox.Show("确实要同时删除此文件吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { foreach (ListViewItem item in lvFileList.SelectedItems) { File.Delete(item.Tag as string); lvFileList.Items.Remove(item); } } } else if (e.KeyCode == Keys.Delete) { foreach (ListViewItem item in lvFileList.SelectedItems) { lvFileList.Items.Remove(item); } } } } } }