첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ RecycleBinFlag.cs

namespace TestProject
{
    /// <summary>
    /// 휴지통 플래그
    /// </summary>
    public enum RecycleBinFlag : uint
    {
        /// <summary>
        /// SHERB_NOCONFIRMATION
        /// </summary>
        SHERB_NOCONFIRMATION = 0x00000001,

        /// <summary>
        /// SHERB_NOPROGRESSUI
        /// </summary>
        SHERB_NOPROGRESSUI = 0x00000002,

        /// <summary>
        /// SHERB_NOSOUND
        /// </summary>
        SHERB_NOSOUND = 0x00000004
    }
}

 

728x90

 

▶ MainForm.cs

using Shell32;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 휴지통 비우기 - SHEmptyRecycleBin(windowHandle, rootPath, recycleBinFlag)

        /// <summary>
        /// 휴지통 비우기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="rootPath">루프 경로</param>
        /// <param name="recycleBinFlag">휴지통 플래그</param>
        /// <returns>처리 결과</returns>
        [DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
        private static extern uint SHEmptyRecycleBin(IntPtr windowHandle, string rootPath, RecycleBinFlag recycleBinFlag);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            LoadRecycleBinFile();
        }

        #endregion

        #region 휴지통 비우기 버튼 클릭시 처리하기 - deleteButton_Click(sender, e)

        /// <summary>
        /// 휴지통 비우기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void deleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                SHEmptyRecycleBin(IntPtr.Zero, null, RecycleBinFlag.SHERB_NOCONFIRMATION);

                MessageBox.Show(this, "휴지통을 비웠습니다.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception exception)
            {
                MessageBox.Show(this, "휴지통 비우는 작업이 실패하였습니다.\n" + exception.Message, "알림", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                return;
            }
            finally
            {
                LoadRecycleBinFile();
            }
        }

        #endregion
        #region 복원 버튼 클릭시 처리하기 - restoreButton_Click(sender, e)

        /// <summary>
        /// 복원 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void restoreButton_Click(object sender, EventArgs e)
        {
            if(this.listView.SelectedItems.Count != 0)
            {
                string fileName              = this.listView.SelectedItems[0].SubItems[1].Text;
                string originalDirectoryPath = this.listView.SelectedItems[0].SubItems[0].Text;
                string originalFilePath      = Path.Combine(originalDirectoryPath, fileName);

                RestoreFile(originalFilePath);

                LoadRecycleBinFile();
            }
            else
            {
                MessageBox.Show("복원할 파일을 선택하세요", "알림", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        #endregion
        #region 새로 고침 버튼 클릭시 처리하기 - refreshButton_Click(sender, e)

        /// <summary>
        /// 새로 고침 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void refreshButton_Click(object sender, EventArgs e)
        {
            LoadRecycleBinFile();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 휴지통 파일 로드하기 - LoadRecycleBinFile()

        /// <summary>
        /// 휴지통 파일 로드하기
        /// </summary>
        private void LoadRecycleBinFile()
        {
            this.listView.Items.Clear();

            Shell shell = new Shell();

            Folder folder = shell.NameSpace(10);

            for(int i = 0; i < folder.Items().Count; i++)
            {
                FolderItem folderItem = folder.Items().Item(i);

                string fileName = folder.GetDetailsOf(folderItem, 0);

                if(Path.GetExtension(fileName) == string.Empty)
                {
                    fileName += Path.GetExtension(folderItem.Path);
                }

                string originalDirectoryPath = folder.GetDetailsOf(folderItem, 1);
                string deleteDate            = folder.GetDetailsOf(folderItem, 2);

                ListViewItem item = new ListViewItem(new string[] { fileName, originalDirectoryPath, deleteDate });

                this.listView.Items.Add(item);
            }
        }

        #endregion
        #region 파일 복원하기 - RestoreFile(originalFilePath)

        /// <summary>
        /// 파일 복원하기
        /// </summary>
        /// <param name="originalFilePath">원본 파일 경로</param>
        /// <returns>처리 결과</returns>
        private bool RestoreFile(string originalFilePath)
        {
            Shell shell = new Shell();

            Folder Recycler = shell.NameSpace(10);

            for(int i = 0; i < Recycler.Items().Count; i++)
            {
                FolderItem folderItem = Recycler.Items().Item(i);

                string fileName = Recycler.GetDetailsOf(folderItem, 0);

                if(Path.GetExtension(fileName) == string.Empty)
                {
                    fileName += Path.GetExtension(folderItem.Path);
                }

                string filePath = Recycler.GetDetailsOf(folderItem, 1);

                if(originalFilePath == Path.Combine(filePath, fileName))
                {
                    DoVerb(folderItem, "복원(&E)");

                    return true;
                }
            }

            return false;
        }

        #endregion
        #region 동사 실행하기 - DoVerb(folderItem, verb)

        /// <summary>
        /// 동사 실행하기
        /// </summary>
        /// <param name="folderItem">폴더 아이템</param>
        /// <param name="verb">동사</param>
        /// <returns>처리 결과</returns>
        private bool DoVerb(FolderItem folderItem, string verb)
        {
            foreach(FolderItemVerb folderItemVerb in folderItem.Verbs())
            {
                if(folderItemVerb.Name.ToUpper().Contains(verb.ToUpper()))
                {
                    folderItemVerb.DoIt();

                    return true;
                }
            }

            return false;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요