■ AppDomain 클래스 : 어셈블리 파일 잠금 없이 어셈블리 버전 구하기

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


TestProject.zip


[TestProject 프로젝트]

 

MainForm.cs

 

 

using System;

using System.IO;

using System.Reflection;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            Load += Form_Load;

        }

 

        #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)

        {

            string executablePath = Path.GetDirectoryName(Application.ExecutablePath);

 

            string filePath = Path.Combine(executablePath, "SourceProject.exe");

 

            Version version = GetVersion(filePath);

 

            this.versionLabel.Text = "SourceProject.exe : " + version.ToString();

        }

 

        #endregion

 

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

 

        #region 버전 구하기 - GetVersion(assemblyFilePath)

 

        /// <summary>

        /// 버전 구하기

        /// </summary>

        /// <param name="assemblyFilePath">어셈블리 파일 경로</param>

        /// <returns>버전</returns>

        private Version GetVersion(string assemblyFilePath)

        {

            AppDomain appDomain = AppDomain.CreateDomain("testDomain");

 

            appDomain.SetData("assemblyFilePath", assemblyFilePath);

 

            appDomain.DoCallBack

            (

                new CrossAppDomainDelegate

                (

                    () => {

 

                        string targetAssemblyFilePath = AppDomain.CurrentDomain.GetData("assemblyFilePath") as string;

 

                        Assembly targetAssembly = Assembly.LoadFile(targetAssemblyFilePath);

 

                        Version targetVersion = targetAssembly.GetName().Version;

 

                        AppDomain.CurrentDomain.SetData("targetVersion", targetVersion);

                    }

                )

            );

 

            Version version = appDomain.GetData("targetVersion") as Version;

 

            AppDomain.Unload(appDomain);

 

            return version;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요