Winform窗体程序 按钮选择文件及存储位置

winform窗体文件中时常需要选择文件及文件存储位置

选择文件

   System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
            fd.Title = "选择文件";//选择框名称        
            fd.Filter = "xls files (*.xlsx)|*.xlsx";//选择文件的类型为Xls表格          
            if (fd.ShowDialog() == DialogResult.OK)//当点击确定               
            {

                SelectFilePath.Text = fd.FileName.Trim();  //文件路径
                                                           //     SelectFilePath.Text = SelectFilePath.Text.Replace("\\", "/");

            }
选择文件

选择多个文件:

  OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;//等于true表示可以选择多个文件
                                   //dlg.DefaultExt = ".txt";
                                   //dlg.Filter = "记事本文件|*.txt";
            dlg.ShowDialog();
            filepath.Text = "已选择" + dlg.FileNames.Length + "个文件";
            str = dlg.FileNames;
选择多个文件

选择文件夹

   FolderBrowserDialog ofd = new FolderBrowserDialog();
            ofd.ShowDialog();
            DownloadPath.Text = ofd.SelectedPath.ToString();
选择文件存储位置

 

posted @ 2021-01-29 11:46  越吃越瘦  阅读(470)  评论(0编辑  收藏  举报