枯岭决 2022-08-31 22:23 采纳率: 100%
浏览 40
已结题

C# 实现 打开资源管理器,选中文件,并上传

用C# 做个档案管理的软件,采用winform + DevExpress 。
现在需要个 “上传文件” 的页面, 打开资源管理器,选中文件后完成上传。如下图:

img


这个页面应该是调用系统的,C# 可以通过 ProcessStartInfo("Explorer.exe") 打开系统的资源管理器,但如何到达上图中的效果呢? (重点是下方的 “打开文件” 功能栏) 求教各位大佬

  • 写回答

3条回答 默认 最新

  • 浪客 2022-09-01 00:28
    关注

    C# winform自带打开对话框,explorer则是win的资源管理器,跟打开没关系

    string file ;
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Multiselect = true;//该值确定是否可以选择多个文件
    dialog.Title = "打开";
    dialog.Filter = "All File(*.*)|*.*";
    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        file = dialog.FileName; //获取到要打开的文件路径
    }
    
    

    具体可参考
    https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.openfiledialog

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
  • 於黾 2022-09-01 08:06
    关注

    如果你需要用户打开文件,可以使用OpenFileDialog,可以自己new,也可以从工具箱拖一个出来放窗体里
    如果你需要用户保存文件,可以使用SaveFileDialog
    如果你需要用户选文件夹,还有FolderBrowserDialog可以用

    评论
  • 丨Haruna 2022-09-01 10:40
    关注
    
    var fileContent = string.Empty;
    var filePath = string.Empty;
    
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
        openFileDialog.InitialDirectory = "c:\\";
        openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog.FilterIndex = 2;
        openFileDialog.RestoreDirectory = true;
    
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            //Get the path of specified file
            filePath = openFileDialog.FileName;
    
            //Read the contents of the file into a stream
            var fileStream = openFileDialog.OpenFile();
    
            using (StreamReader reader = new StreamReader(fileStream))
            {
                fileContent = reader.ReadToEnd();
            }
        }
    }
    
    MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
    
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月9日
  • 已采纳回答 9月1日
  • 创建了问题 8月31日

悬赏问题

  • ¥15 AD20 PCB板步线 这个要怎么步啊
  • ¥50 关于《奇迹世界》1.5版本,数据修改
  • ¥15 请问这个问题如何解决(关键词-File)
  • ¥50 visual studio 2022和EasyX图形化界面
  • ¥15 找一下报错原因,纠正一下
  • ¥50 Cox回归模型Nomogram图制作报错
  • ¥20 SQL如何查询多级用户的数据
  • ¥15 给车牌识别代码加一个识别轮廓长宽比的代码
  • ¥30 商品价格预测的transformer模型优化
  • ¥15 SQL中的图片,无法传回php到前端。没有任何报错。