728x90

C# 에서 자동 업데이터를 만들어보고 있는데 Program Files(x86) 폴더 하단에 설치된 파일은 자동 업데이터를 구동시켜 보니까 Access 권한이 없다고 나온다.


Program.cs 파일 내용을 아래와 같이 수정해주면

파일 실행시 관리자 권한으로 실행할 것인지를 물어본다.

컴파일한 파일을 Program Files(x86) 폴더 해당폴더에 복사하고 나서 실행하니까 웹에서 파일을 다운로드 받아서 해당 폴더 파일을 업데이트한다.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security.Principal;

namespace AutoUpater
{
    static class Program
    {
        /// <summary>
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// </summary>
        [STAThread]
        static void Main()
        {
            if (IsAdministrator() == false)
            {
                try
                {
                    ProcessStartInfo procInfo = new ProcessStartInfo();
                    procInfo.UseShellExecute = true;
                    procInfo.FileName = Application.ExecutablePath;
                    procInfo.WorkingDirectory = Environment.CurrentDirectory;
                    procInfo.Verb = "runas";
                    Process.Start(procInfo);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        public static bool IsAdministrator()
        {
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            if (null != identity)
            {
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                return principal.IsInRole(WindowsBuiltInRole.Administrator);
            }
            return false;
        }

    }
}




블로그 이미지

Link2Me

,