■ 단축 아이콘 정보 조회하기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 이벤트를 설정한다.

 

            this.searchButton.Click += searchButton_Click;

 

            #endregion

        }

 

        #endregion

 

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

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

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

 

        #region 조회 버튼 클릭시 처리하기 - searchButton_Click(sender, e)

 

        /// <summary>

        /// 조회 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void searchButton_Click(object sender, EventArgs e)

        {

            string name;

            string description;

            string path;

            string workingDirectoryPath;

            string argument;

 

            string result = GetShortcutIconInformation(shortcutTextBox.Text, out name, out description, out path,

                out workingDirectoryPath, out argument);

 

            if(result.Length > 0)

            {

                MessageBox.Show

                (

                    this,

                    result,

                    "INFORMATION",

                    MessageBoxButtons.OK,

                    MessageBoxIcon.Information

                );

            }

 

            this.nameTextBox.Text                 = name;

            this.descriptionTextBox.Text          = description;

            this.pathTextBox.Text                 = path;

            this.workingDirectoryPathTextBox.Text = workingDirectoryPath;

            this.argumentTextBox.Text             = argument;

        }

 

        #endregion

 

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

 

        #region 단축 아이콘 정보 구하기 - GetShortcutIconInformation(shortcutIconFilePath, name, description, path,

            workingDirectoryPath, argument)

 

        /// <summary>

        /// 단축 아이콘 정보 구하기

        /// </summary>

        /// <param name="shortcutIconFilePath">단축 아이콘 파일 경로</param>

        /// <param name="name">명칭</param>

        /// <param name="description">설명</param>

        /// <param name="path">경로</param>

        /// <param name="workingDirectoryPath">작업 디렉토리 경로</param>

        /// <param name="argument">인자</param>

        /// <returns>처리 결과</returns>

        private string GetShortcutIconInformation(string shortcutIconFilePath, out string name, out string description, out string path,

            out string workingDirectoryPath, out string argument)

        {

            name                 = string.Empty;

            description          = string.Empty;

            path                 = string.Empty;

            workingDirectoryPath = string.Empty;

            argument             = string.Empty;

 

            try

            {

                Shell32.Shell shell = new Shell32.Shell();

 

                string shortcutIconDirectoryPath = shortcutIconFilePath.Substring(0, shortcutIconFilePath.LastIndexOf("\\"));

                string shortcutIconFileName      = shortcutIconFilePath.Substring(shortcutIconFilePath.LastIndexOf("\\") + 1);

 

                if(!shortcutIconFileName.EndsWith(".lnk"))

                {

                    shortcutIconFileName += ".lnk";

                }

 

                Shell32.Folder shortcutFolder = shell.NameSpace(shortcutIconDirectoryPath);

 

                Shell32.FolderItem folderItem = shortcutFolder.Items().Item(shortcutIconFileName);

 

                if(folderItem == null)

                {

                    return $"단축 아이콘 파일을 찾을 수 없습니다 : {shortcutIconFilePath}";

                }

 

                if(!folderItem.IsLink)

                {

                    return $"단축 아이콘 파일이 아닙니다 : {shortcutIconFilePath}";

                }

 

                Shell32.ShellLinkObject shellLinkObject = (Shell32.ShellLinkObject)folderItem.GetLink;

 

                name                 = folderItem.Name;

                description          = shellLinkObject.Description;

                path                 = shellLinkObject.Path;

                workingDirectoryPath = shellLinkObject.WorkingDirectory;

                argument             = shellLinkObject.Arguments;

 

                return string.Empty;

            }

            catch(Exception exception)

            {

                return exception.Message;

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요