■ 파일 확장자와 연결된 실행 파일 경로 구하기

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


TestProject.zip


FileAssociationHelper.cs

 

 

using System;

using System.Collections.Generic;

using System.Runtime.InteropServices;

using System.Text;

 

namespace TestProject

{

    /// <summary>

    /// 파일 연결 헬퍼

    /// </summary>

    public static class FileAssociationHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Import

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

        #region 연결 쿼리 문자열 구하기 - AssocQueryString(associationFlag, associationString, association, extraAssociation,

            resultStringBuilder, resultLength)

 

        /// <summary>

        /// 연결 쿼리 문자열 구하기

        /// </summary>

        /// <param name="associationFlag">연결 플래그</param>

        /// <param name="associationString">연결 문자열</param>

        /// <param name="association">연결</param>

        /// <param name="extraAssociation">부가 연결</param>

        /// <param name="resultStringBuilder">결과 문자열 빌더</param>

        /// <param name="resultLength">결과 길이</param>

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

        [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]

        private static unsafe extern uint AssocQueryString(AssocicationFlag associationFlag, AssociationString associationString,

            string association, string extraAssociation, [Out] StringBuilder resultStringBuilder, ref uint resultLength);

 

        #endregion

 

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

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

        #region 값 딕셔너리 구하기 - GetValueDictionary(fileExtension)

 

        /// <summary>

        /// 값 딕셔너리 구하기

        /// </summary>

        /// <param name="fileExtension">파일 확장자</param>

        /// <returns>값 딕셔너리</returns>

        public unsafe static Dictionary<string, string> GetValueDictionary(string fileExtension)

        {

            Dictionary<string, string> dictionary = new Dictionary<string, string>();

 

            StringBuilder stringBuilder = new StringBuilder(1024);

 

            foreach(string name in Enum.GetNames(typeof(AssociationString)))

            {

                uint count = 512;

 

                AssociationString associationString = (AssociationString)Enum.Parse(typeof(AssociationString), name);

 

                uint result = AssocQueryString(AssocicationFlag.None, associationString, fileExtension, null, stringBuilder, ref count);

 

                if(result == 0)

                {

                    dictionary.Add(name, stringBuilder.ToString());

                }

            }

 

            return dictionary;

        }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

using System.Collections.Generic;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

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

        ////////////////////////////////////////////////////////////////////////////////////////// Static

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

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        [STAThread]

        private static void Main()

        {

            Console.Title = "파일 확장자와 연결된 실행 파일 경로 구하기";

 

            Dictionary<string, string> dictionary = FileAssociationHelper.GetValueDictionary(@".mp3");

 

            foreach(KeyValuePair<string, string> keyValuePair in dictionary)

            {

                Console.WriteLine($"{keyValuePair.Key} : {keyValuePair.Value}");

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요