Code/C#

Path 클래스를 사용해서 원하는 파일의 위치를 지정하는 방법

Hide Code 2007. 12. 15. 21:18
현재 실행 중인 어플리케이션과 같은 폴더에 있는 파일이나, 하위 폴더에 있는 파일의 위치를 지정하고자 한다면, Path 클래스를 사용하면 된다.

아래는 간단한 예제이다.

Application.ExecutablePath 은 현재 실행 중인 어플리케이션의 위치를 반환한다.

Path.GetDirectoryName 은 path에서 폴더 값만 반환한다.

Path.Combine 은 두개의 path를 결합한다.


using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click(object sender, EventArgs e)
       {
           textBox1.Text = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "file.txt");
       }
   }
}