C# 複製 IE 緩存文件

using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinFormIECache
{
    public partial class FormIECache : Form
    {
        #region AnimateWindow
        [DllImport("user32.dll", EntryPoint = "AnimateWindow")]
        private static extern bool AnimateWindow(IntPtr handle, int ms, int flags);
        #endregion

        private ChineseLunisolarCalendar lunarCalendar;

        public FormIECache()
        {
            #region
            InitializeComponent();
            lunarCalendar = new ChineseLunisolarCalendar();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = Environment.UserName;
            listViewFile.MultiSelect = true;
            listViewFile.ShowItemToolTips = true;
            listViewFile.Sorting = SortOrder.Ascending;
            listViewFile.View = View.List;
            #endregion
        }

        #region FormLoad
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            AnimateWindow(this.Handle, 1000, 0x20010);
            toolButtonRefresh_Click(null, EventArgs.Empty);
        }
        #endregion

        #region FormClosing
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            AnimateWindow(this.Handle, 1000, 0x10010);
        }
        #endregion

        #region DateTimeFormatInfo
        private void timerInfo_Tick(object sender, EventArgs e)
        {
            Application.CurrentCulture.ClearCachedData();
            DateTime solar = DateTime.Now;
            int month = lunarCalendar.GetMonth(solar);
            int leapMonth = lunarCalendar.GetLeapMonth(lunarCalendar.GetYear(solar));
            if (0 < leapMonth && leapMonth <= month)
                --month;
            statusLabelTime.Text = string.Format("{0:dddd yyyy'年'M'月'd'日' tt h:mm:ss} [{1} {2:00}]", solar, DateTimeFormatInfo.CurrentInfo.MonthNames[month - 1], lunarCalendar.GetDayOfMonth(solar));
        }
        #endregion

        #region Refresh
        private void toolButtonRefresh_Click(object sender, EventArgs e)
        {
            string dirPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
            DirectoryInfo dir = new DirectoryInfo(dirPath);
            var query = from info in dir.GetFiles("*.*", SearchOption.AllDirectories)
                        let Extension = info.Extension.ToLowerInvariant()
                        let Hidden = info.Attributes & FileAttributes.Hidden
                        where Hidden == 0 && 2 < Extension.Length && Extension.Length < 6
                        group "" by Extension into Groups
                        orderby Groups.Key ascending
                        select Groups; // 根據文件擴展名分組排序。
            toolComboBoxExt.Items.Clear();
            toolComboBoxExt.BeginUpdate();
            foreach (var files in query)
            {
                toolComboBoxExt.Items.Add("*" + files.Key);
            }
            toolComboBoxExt.EndUpdate();
        }
        #endregion

        #region OpenFile
        private void listViewFile_DoubleClick(object sender, EventArgs e)
        {
            if (listViewFile.FocusedItem == null)
                return;
            using (Process psi = new Process())
            {
                psi.StartInfo.FileName = "explorer.exe";
                psi.StartInfo.Arguments = listViewFile.FocusedItem.Tag as string;
                psi.Start();
            }
        }

        private void toolButtonOpen_Click(object sender, EventArgs e)
        {
            Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
        }
        #endregion

        #region LoadFiles
        private void toolComboBoxExt_SelectedIndexChanged(object sender, EventArgs e)
        {
            listViewFile.Items.Clear();
            listViewFile.BeginUpdate();
            string dirPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
            DirectoryInfo dir = new DirectoryInfo(dirPath);
            foreach (FileInfo info in dir.GetFiles(toolComboBoxExt.Text, SearchOption.AllDirectories))
            {
                ListViewItem item = listViewFile.Items.Add(info.Name);
                item.ToolTipText = string.Format("名稱: {0}\n大小: {1:#,##0.00} KB\n創建日期: {2:yyyy-MM-dd HH:mm}", info.Name, info.Length / 1024M, info.CreationTime);
                item.Tag = info.FullName;
                this.Text = string.Format("正在加載文件 {0}", listViewFile.Items.Count);
            }
            listViewFile.EndUpdate();
            this.Text = string.Format("已加載文件 {0}", listViewFile.Items.Count);
        }
        #endregion

        #region CopyFiles
        private void toolMenuItemCopy_Click(object sender, EventArgs e)
        {
            if (folderBrowser.ShowDialog(this) == DialogResult.OK)
            {
                foreach (ListViewItem item in listViewFile.SelectedItems)
                {
                    File.Copy(item.Tag as string, Path.Combine(folderBrowser.SelectedPath, item.Text), true);
                }
                Process.Start(folderBrowser.SelectedPath);
            }
            else
            {
                StringCollection sc = new StringCollection();
                foreach (ListViewItem item in listViewFile.SelectedItems)
                {
                    sc.Add(item.Tag as string);
                }
                Clipboard.SetFileDropList(sc);
                sc.Clear();
                sc = null;
            }
        }

        private void toolButtonCopy_Click(object sender, EventArgs e)
        {
            if (toolComboBoxExt.SelectedIndex < 0)
                return;
            DirectoryInfo dir = Directory.CreateDirectory(toolComboBoxExt.Text.Remove(0, 2));
            foreach (ListViewItem item in listViewFile.Items)
            {
                File.Copy(item.Tag as string, Path.Combine(dir.Name, item.Text), true);
                this.Text = string.Format("正在複製文件 {0}/{1}", item.Index + 1, listViewFile.Items.Count);
            }
            this.Text = string.Format("已複製文件 {0}", listViewFile.Items.Count);
            Process.Start(dir.Name);
        }
        #endregion

        #region DeleteFiles
        private void toolMenuItemDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(string.Format("確實要刪除文件“{0}”嗎?", listViewFile.SelectedIndices.Count), "確認文件刪除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;
            foreach (ListViewItem item in listViewFile.SelectedItems)
            {
                try
                {
                    File.Delete(item.Tag as string);
                    item.Remove();
                }
                catch
                {
                    continue;
                }
            }
            this.Text = Environment.UserName;
        }

        private void toolButtonClear_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(string.Format("確實要刪除文件“{0}”嗎?", listViewFile.Items.Count), "確認文件刪除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;
            foreach (ListViewItem item in listViewFile.Items)
            {
                try
                {
                    File.Delete(item.Tag as string);
                    item.Remove();
                }
                catch
                {
                    continue;
                }
            }
            this.Text = Environment.UserName;
        }
        #endregion

        #region ContextMenuVisible
        private void contextMenuInfo_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = (listViewFile.SelectedIndices.Count < 1);
        }
        #endregion
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章