c#怎么获取用户选择文件路径

时间:2026-02-14 14:59:53

1、// 选择文件:

private string SelectPath()

{

    string path = string.Empty;

    var openFileDialog = new Microsoft.Win32.OpenFileDialog()

    {

        Filter = "Files (*.*)|*.*"//如果需要筛选txt文件("Files (*.txt)|*.txt")

    };

    var result = openFileDialog.ShowDialog();

    if (result == true)

    {

        path = openFileDialog.FileName;

    }

    return path

}

// 选择路径

private string SelectPath()

{

    string path = string.Empty;

    System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();

    if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)

    {

        path = fbd.SelectedPath;

    }

    return path;

}

© 2026 海能知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com