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;

}

  • Windows11如何设置当前播放声音的所有设备
  • C# 隐藏、显示Excel工作表、行或列
  • git bash怎样创建版本库?
  • 用C#实现带箭头的直线,可调整箭头的大小
  • C#如何设置启动对象
  • 热门搜索
    微信朋友圈访客记录怎么看 笔仙游戏怎么玩 脂肪肝如何治疗和调养 qq飞车怎么刷点卷 ps如何填充颜色 桃花妖哪里多 欧舒丹怎么样 比亚迪思锐怎么样 卡塔尔在哪里 河南红色旅游景点