첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ Directory 클래스 : SetAccessControl 정적 메소드를 사용해 폴더 잠금(Folder Lock) 설정하기

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


TestProject.zip


MainForm.cs

 

 

using System;

using System.IO;

using System.Security.AccessControl;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm: Form

    {

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

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

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            this.selectFolderButton.Click += selectFolderButton_Click;

            this.lockFolderButton.Click   += lockFolderButton_Click;

            this.UnlockFolderButton.Click += UnlockFolderButton_Click;

        }

 

        #endregion

 

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

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

 

        #region 폴더 선택 버튼 클릭시 처리하기 - selectFolderButton_Click(sender, e)

 

        /// <summary>

        /// 폴더 선택 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void selectFolderButton_Click(object sender, EventArgs e)

        {

            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

            

            if(folderBrowserDialog.ShowDialog() == DialogResult.OK)

            {

                this.folderLabel.Text = folderBrowserDialog.SelectedPath;

            }

        }

 

        #endregion

        #region 폴더 잠금 버튼 클릭시 처리하기 - lockFolderButton_Click(sender, e)

 

        /// <summary>

        /// 폴더 잠금 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void lockFolderButton_Click(object sender, EventArgs e)

        {

            if(!Directory.Exists(this.folderLabel.Text))

            {

                return;

            }

            

            try

            {

                string identity = Environment.UserName;

 

                DirectorySecurity directorySecurity = Directory.GetAccessControl(this.folderLabel.Text);

 

                FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule

                (

                    identity,

                    FileSystemRights.FullControl,

                    AccessControlType.Deny

                );

 

                directorySecurity.AddAccessRule(fileSystemAccessRule);

 

                Directory.SetAccessControl(this.folderLabel.Text, directorySecurity);

            }

            catch(Exception exception)

            {

                this.resultValueLabel.Text = exception.ToString();

            }

            

            this.resultValueLabel.Text = "폴더 잠금...";

        }

 

        #endregion

        #region 폴더 잠금 해제 버튼 클릭시 처리하기 - UnlockFolderButton_Click(sender, e)

 

        /// <summary>

        /// 폴더 잠금 해제 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void UnlockFolderButton_Click(object sender, EventArgs e)

        {

            if(!Directory.Exists(this.folderLabel.Text))

            {

                return;

            }

            

            try

            {

                string identity = Environment.UserName;

 

                DirectorySecurity directorySecurity = Directory.GetAccessControl(this.folderLabel.Text);

 

                FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule

                (

                    identity,

                    FileSystemRights.FullControl,

                    AccessControlType.Deny

                );

 

                directorySecurity.RemoveAccessRule(fileSystemAccessRule);

 

                Directory.SetAccessControl(this.folderLabel.Text, directorySecurity);

            }

            catch(Exception exception)

            {

                this.resultValueLabel.Text = exception.ToString();

            }

 

            this.resultValueLabel.Text = "파일 잠금 해제...";

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요