using System;

using System.Windows.Forms;

 

namespace LogReader

{

    public partial class Main : Form

    {

 

        public Main()

        {

            InitializeComponent();    

        }

 

        private void tsm_Open_Click(object sender, EventArgs e)

        {

            ShowFileOpenDialog();

        }

 

        /// <summary>

        /// 그림파일오픈창을 로드후 해당 파일의 FullPath를 가져온다.

        /// </summary>

        /// <returns>파일의 FullPath 파일이 없거나 선택을 안할경우 ""를 리턴</returns>

        public string ShowFileOpenDialog()

        {

            //파일오픈창 생성 및 설정

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Title = "파일 오픈 예제창";

            ofd.FileName = "test";

            ofd.Filter = "그림 파일 (*.jpg, *.gif, *.bmp) | *.jpg; *.gif; *.bmp; | 모든 파일 (*.*) | *.*";

            //필터설명|필터옵션|필터설명|필터옵션|......


            //파일 오픈창 로드

            DialogResult dr = ofd.ShowDialog();

             

            //OK버튼 클릭시

            if (dr == DialogResult.OK)

            {

                //File명과 확장자를 가지고 온다.

                string fileName = ofd.SafeFileName;

                //File경로와 File명을 모두 가지고 온다.

                string fileFullName = ofd.FileName;

                //File경로만 가지고 온다.

                string filePath = fileFullName.Replace(fileName, "");

 

                //출력 예제용 로직

                label1.Text = "File Name  : " + fileName;

                label2.Text = "Full Name  : " + fileFullName;

                label3.Text = "File Path  : " + filePath;

                //File경로 + 파일명 리턴

                return fileFullName;

            }

            //취소버튼 클릭시 또는 ESC키로 파일창을 종료 했을경우

            else if (dr == DialogResult.Cancel)

            {

                return "";

            }

 

            return "";

        }

 

         

    }

}





출처: http://mirwebma.tistory.com/121 [Run and Fly]

'Programming > C#' 카테고리의 다른 글

c# 동적으로 textbox 생성하기  (0) 2017.08.05
winform 파일 쓰기  (0) 2017.08.05
WinForm과 WPF  (0) 2017.07.02
윈폼(WinForm 의 구성요소, 생성코드)  (0) 2017.07.02
DLL 파일  (0) 2017.07.02

+ Recent posts