■ 특정 프로세스에서 오픈한 파일과 핸들 리스트 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Diagnostics;

using System.IO;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 파일 스트림

        /// </summary>

        private FileStream fileStream;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.fileStream = File.OpenRead("Sample.txt");

 

            this.listView.Columns.Add("타입", 120);

            this.listView.Columns.Add("명칭", 640);

 

            this.fileButton.Click   += fileButton_Click;

            this.handleButton.Click += handleButton_Click;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

 

        #region 닫을 경우 처리하기 - OnClosing(e)

 

        /// <summary>

        /// 닫을 경우 처리하기

        /// </summary>

        /// <param name="e">이벤트 발생자</param>

        protected override void OnClosing(CancelEventArgs e)

        {

            base.OnClosing(e);

 

            this.fileStream.Close();

        }

 

        #endregion

 

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 파일 버튼 클릭시 처리하기 - fileButton_Click(sender, e)

 

        /// <summary>

        /// 파일 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void fileButton_Click(object sender, EventArgs e)

        {

            try

            {

                int processID = GetProcessID();

 

                this.listView.BeginUpdate();

 

                this.listView.Items.Clear();

 

                using(IEnumerator<FileSystemInfo> enumerator = OpenFileHelper.GetOpenFileEnumerator(processID))

                {

                    if(enumerator != null)

                    {

                        while(enumerator.MoveNext())

                        {

                            FileSystemInfo info = enumerator.Current;

 

                            ListViewItem item = this.listView.Items.Add("File");

 

                            item.SubItems.Add(info.FullName);

                        }

                    }

                }

 

                this.listView.EndUpdate();

            }

            catch

            {

            }

        }

 

        #endregion

        #region 핸들 버튼 클릭시 처리하기 - handleButton_Click(sender, e)

 

        /// <summary>

        /// 핸들 버튼 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void handleButton_Click(object sender, EventArgs e)

        {

            try

            {

                int processID = GetProcessID();

 

                this.listView.BeginUpdate();

 

                this.listView.Items.Clear();

 

                using(IEnumerator<HandleInfo> enumerator = OpenFileHelper.GetOpenHandleEnumerator(processID))

                {

                    if(enumerator != null)

                    {

                        while(enumerator.MoveNext())

                        {

                            HandleInfo info = enumerator.Current;

 

                            ListViewItem item = this.listView.Items.Add(info.Type.ToString());

 

                            item.SubItems.Add(info.Name);

                        }

                    }

                }

 

                this.listView.EndUpdate();

            }

            catch

            {

            }

        }

 

        #endregion

 

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

 

        #region 프로세스 ID 구하기 - GetProcessID()

 

        /// <summary>

        /// 프로세스 ID 구하기

        /// </summary>

        /// <returns>프로세스 ID</returns>

        private int GetProcessID()

        {

            Process process = Process.GetCurrentProcess();

 

            int processID = process.Id;

 

            if(!string.IsNullOrEmpty(this.processIDTextBox.Text))

            {

                processID = int.Parse(this.processIDTextBox.Text);

            }

 

            return processID;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요