본문 바로가기
2/[ C# ]

[C#] openFileDialog, 파일 불러오기

by Kieran_Han 2023. 12. 6.

1. 도구상자에서 "openFileDialgo"를 Form 內 Drag

2. "File Browser" 버튼을 더블 클릭하여 Event 활성화

3. 아래 코드 입력

private void btnFileBrowser_Click(object sender, EventArgs e)
{
    tboxFileAddress.Clear();
    String file_path = null;

    // Current User's Desktop Address
    string localpath = Environment.GetFolderPath (Environment.SpecialFolder.Desktop);
    openFileDialog1.InitialDirectory = localpath;

    // Set openFileDialog's initial address is User's Desktop
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        // Save the seleted file's path
        file_path = openFileDialog1.FileName;

        // Display the loaded file name on the textbox
        tboxFileAddress.Text = file_path.Split("\\")[file_path.Split("\\").Length - 1];
    }
}