첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ FileSystemHelper.cs

using System.IO;

namespace TestProject
{
    /// <summary>
    /// 파일 시스템 도우미
    /// </summary>
    public class FileSystemHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 논리 드라이브 배열 구하기 - GetLogicalDriveArray()

        /// <summary>
        /// 논리 드라이브 배열 구하기
        /// </summary>
        /// <returns>논리 드라이브 배열</returns>
        public string[] GetLogicalDriveArray()
        {
            return Directory.GetLogicalDrives();
        }

        #endregion
        #region 디렉토리 경로 배열 구하기 - GetDirectoryPathArray(parentDirectoryPath)

        /// <summary>
        /// 디렉토리 경로 배열 구하기
        /// </summary>
        /// <param name="parentDirectoryPath">부모 디렉토리 경로</param>
        /// <returns>디렉토리 경로 배열</returns>
        public string[] GetDirectoryPathArray(string parentDirectoryPath)
        {
            return Directory.GetDirectories(parentDirectoryPath);
        }

        #endregion
        #region 파일 경로 배열 구하기 - GetFilePathArray(directoryPath)

        /// <summary>
        /// 파일 경로 배열 구하기
        /// </summary>
        /// <param name="directoryPath">디렉토리 경로</param>
        /// <returns>파일 경로 배열</returns>
        public string[] GetFilePathArray(string directoryPath)
        {
            return Directory.GetFiles(directoryPath);
        }

        #endregion
        #region 디렉토리명 구하기 - GetDirectoryName(directoryPath)

        /// <summary>
        /// 디렉토리명 구하기
        /// </summary>
        /// <param name="directoryPath">디렉토리 경로</param>
        /// <returns>디렉토리명</returns>
        public string GetDirectoryName(string directoryPath)
        {
            return new DirectoryInfo(directoryPath).Name;
        }

        #endregion
        #region 파일명 구하기 - GetFileName(filePath)

        /// <summary>
        /// 파일명 구하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>파일명</returns>
        public string GetFileName(string filePath)
        {
            return new FileInfo(filePath).Name;
        }

        #endregion
        #region 파일 크기 구하기 - GetFileSize(filePath)

        /// <summary>
        /// 파일 크기 구하기
        /// </summary>
        /// <param name="filePath">파일 경로</param>
        /// <returns>파일 크기</returns>
        public long GetFileSize(string filePath)
        {
            return new FileInfo(filePath).Length;
        }

        #endregion
    }
}

 

728x90

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    Width="800"
    Height="600"
    Title="TreeListView 클래스 : 동적으로 노드 로드하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <dxg:GridControl Name="gridControl">
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="Name"     />
                <dxg:GridColumn FieldName="ItemType" />
                <dxg:GridColumn FieldName="Size"     />
                <dxg:GridColumn FieldName="FullName" />
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TreeListView
                    Name="treeListView"
                    NodeExpanding="treeListView_NodeExpanding" />
            </dxg:GridControl.View>
        </dxg:GridControl>
    </Grid>
</Window>

 

300x250

 

▶ MainWindow.xaml.cs

using System.Windows;

using DevExpress.Utils;
using DevExpress.Xpf.Grid;
using DevExpress.Xpf.Grid.TreeList;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 파일 시스템 도우미
        /// </summary>
        private FileSystemHelper fileSystemHelper = new FileSystemHelper();

        #endregion

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

        #region 생성자 - MainWindow()

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

            InitializeDrive();
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 트리 리스트 뷰 노드 확장시 처리하기 - treeListView_NodeExpanding(sender, e)

        /// <summary>
        /// 트리 리스트 뷰 노드 확장시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void treeListView_NodeExpanding(object sender, TreeListNodeAllowEventArgs e)
        {
            TreeListNode treeListNode = e.Node;

            if(treeListNode.Tag == null || (bool)treeListNode.Tag == false)
            {
                BuildChildNode(treeListNode);

                treeListNode.Tag = true;
            }
        }

        #endregion
        #region 드라이브 초기화 하기 - InitializeDrive()

        /// <summary>
        /// 드라이브 초기화 하기
        /// </summary>
        public void InitializeDrive()
        {
            this.gridControl.BeginDataUpdate();

            try
            {
                string[] driveArray = this.fileSystemHelper.GetLogicalDriveArray();

                foreach(string drive in driveArray)
                {
                    TreeListNode rootNode = new TreeListNode() { Content = new FileSystemItem(drive, "Drive", "<Drive>", drive) };

                    this.treeListView.Nodes.Add(rootNode);

                    rootNode.IsExpandButtonVisible = DefaultBoolean.True;
                }
            }
            catch
            {
            }

            this.gridControl.EndDataUpdate();
        }

        #endregion
        #region 자식 노드 구축하기 - BuildChildNode(parentNode)

        /// <summary>
        /// 자식 노드 구축하기
        /// </summary>
        /// <param name="parentNode">부모 노드</param>
        private void BuildChildNode(TreeListNode parentNode)
        {
            this.gridControl.BeginDataUpdate();

            BuildFolderChildNode(parentNode);

            BuildFileChildNode(parentNode);

            this.gridControl.EndDataUpdate();
        }

        #endregion
        #region 파일 자식 노드 구축하기 - BuildFileChildNode(parentNode)

        /// <summary>
        /// 파일 자식 노드 구축하기
        /// </summary>
        /// <param name="parentNode">부모 노드</param>
        private void BuildFileChildNode(TreeListNode parentNode)
        {
            FileSystemItem fileSystemItem = parentNode.Content as FileSystemItem;

            if(fileSystemItem == null)
            {
                return;
            }

            try
            {
                string[] filePathArray = this.fileSystemHelper.GetFilePathArray(fileSystemItem.FullName);

                foreach(string filePath in filePathArray)
                {
                    TreeListNode childNode = new TreeListNode()
                    {
                        Content = new FileSystemItem
                        (
                            this.fileSystemHelper.GetFileName(filePath),
                            "File",
                            this.fileSystemHelper.GetFileSize(filePath).ToString(),
                            filePath
                        )
                    };

                    childNode.IsExpandButtonVisible = DefaultBoolean.False;

                    parentNode.Nodes.Add(childNode);
                }
            }
            catch
            {
            }
        }

        #endregion
        #region 폴더 자식 노드 구축하기 - BuildFolderChildNode(parentNode)

        /// <summary>
        /// 폴더 자식 노드 구축하기
        /// </summary>
        /// <param name="parentNode">부모 노드</param>
        private void BuildFolderChildNode(TreeListNode parentNode)
        {
            FileSystemItem fileSystemItem = parentNode.Content as FileSystemItem;

            if(fileSystemItem == null)
            {
                return;
            }

            try
            {
                string[] directoryPathArray = this.fileSystemHelper.GetDirectoryPathArray(fileSystemItem.FullName);

                foreach(string directoryPath in directoryPathArray)
                {
                    try
                    {
                        TreeListNode childNode = new TreeListNode()
                        {
                            Content = new FileSystemItem
                            (
                                this.fileSystemHelper.GetDirectoryName(directoryPath),
                                "Folder",
                                "<Folder>",
                                directoryPath
                            )
                        };

                        parentNode.Nodes.Add(childNode);

                        childNode.IsExpandButtonVisible = HasChildren(directoryPath) ? DefaultBoolean.True : DefaultBoolean.False;
                    }
                    catch
                    {
                    }
                }
            }
            catch
            {
            }
        }

        #endregion
        #region 자식 보유 여부 조사하기 - HasFiles(directoryPath)

        /// <summary>
        /// 자식 보유 여부 조사하기
        /// </summary>
        /// <param name="directoryPath">디렉토리 경로</param>
        /// <returns>자식 보유 여부</returns>
        private bool HasChildren(string directoryPath)
        {
            string[] filePathArray = this.fileSystemHelper.GetFilePathArray(directoryPath);

            if(filePathArray.Length > 0)
            {
                return true;
            }

            string[] childDirectoryPathArray = this.fileSystemHelper.GetDirectoryPathArray(directoryPath);

            if(childDirectoryPathArray.Length > 0)
            {
                return true;
            }

            return false;
        }

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

댓글을 달아 주세요