本文介绍了如何从打开文件对话框,并获得的FolderBrowserDialog文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 嘿那里我开始学习C#前几天,我试图让一个程序,复制并粘贴文件(如果需要更换)到选定的目录,但我不知道怎么弄的目录和文件从打开文件对话框路径和的FolderBrowserDialog 我在做什么错 下面的代码: 命名空间filereplacer {公共部分Form1类:表格 {公共Form1中() {的InitializeComponent(); } 私人无效direc_Click(对象发件人,EventArgs五) {串FOLDERPATH =; 的FolderBrowserDialog directchoosedlg =新的FolderBrowserDialog(); 如果(directchoosedlg.ShowDialog()== DialogResult.OK) { FOLDERPATH = directchoosedlg.SelectedPath; } } 私人无效choof_Click(对象发件人,EventArgs五) { 打开文件对话框choofdlog =新的OpenFileDialog(); choofdlog.Filter =所有文件(*。*)| *。*; choofdlog.FilterIndex = 1; choofdlog.Multiselect = TRUE; choofdlog.ShowDialog(); } 私人无效replacebtn_Click(对象发件人,EventArgs五) { //这是我遇到的麻烦} 酒店的公共静态无效ReplaceFile(字符串FileToMoveAndDelete,串FileToReplace,串BackupOfFileToReplace) { File.Replace(FileToMoveAndDelete,FileToReplace,BackupOfFileToReplace,FALSE); } } 解决方案 打开文件对话框 打开文件对话框choofdlog =新的OpenFileDialog(); choofdlog.Filter =所有文件(*。*)| *。*; choofdlog.FilterIndex = 1; choofdlog.Multiselect = TRUE; 如果(choofdlog.ShowDialog()== DialogResult.OK) {字符串sFileName = choofdlog.FileName; 的String [] = arrAllFiles choofdlog.FileNames; //使用时多选= TRUE } 有关的FolderBrowserDialog 的FolderBrowserDialog FBD =新的FolderBrowserDialog(); fbd.Description =自定义说明; 如果(fbd.ShowDialog()== DialogResult.OK) {串sSelectedPath = fbd.SelectedPath; } 要访问选定的文件夹和选定的文件名您可以在一流水平 命名空间filereplacer {公共部分Form1类:表格 {串sSelectedFile; 串sSelectedFolder; 公共Form1中() {的InitializeComponent(); } 私人无效direc_Click(对象发件人,EventArgs五) {的FolderBrowserDialog FBD =新的FolderBrowserDialog(); //fbd.Description =自定义说明; //不是强制性的 如果(fbd.ShowDialog()== DialogResult.OK) sSelectedFolder = fbd.SelectedPath; ,否则 sSelectedFolder =的String.Empty; } 私人无效choof_Click(对象发件人,EventArgs五) {打开文件对话框choofdlog =新的OpenFileDialog(); choofdlog.Filter =所有文件(*。*)| *。*; choofdlog.FilterIndex = 1; choofdlog.Multiselect = TRUE; 如果(choofdlog.ShowDialog()== DialogResult.OK) sSelectedFile = choofdlog.FileName; ,否则 sSelectedFile =的String.Empty; } 私人无效replacebtn_Click(对象发件人,EventArgs五) {如果(sSelectedFolder =&的String.Empty放大器;!&安培;!sSelectedFile =的String.Empty ) { //使用选定的文件夹路径和文件路径} } .... } 注意 如您保持 choofdlog.Multiselect = TRUE; ,这意味着在打开文件对话框()您可以选择多个文件(按 CTRL 键和鼠标左键点击选择)。 在这种情况下,你可以得到在的String [] : 目前职业等级: 的String [] arrAllFiles; 找到这条线(当多选= TRUE 该行给出了第一个文件只): sSelectedFile = choofdlog.FileName; 要获得所有文件使用这样的: arrAllFiles = choofdlog.FileNames; //此行给出了所有选定的文件 数组Hey there i started learning C# a few days ago and I'm trying to make a program that copies and pastes files (and replaces if needed) to a selected directory but I don't know how to get the directory and file paths from the openfiledialog and folderbrowserdialogwhat am I doing wrong?Here's the code:namespace filereplacer{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void direc_Click(object sender, EventArgs e) { string folderPath = ""; FolderBrowserDialog directchoosedlg = new FolderBrowserDialog(); if (directchoosedlg.ShowDialog() == DialogResult.OK) { folderPath = directchoosedlg.SelectedPath; } } private void choof_Click(object sender, EventArgs e) { OpenFileDialog choofdlog = new OpenFileDialog(); choofdlog.Filter = "All Files (*.*)|*.*"; choofdlog.FilterIndex = 1; choofdlog.Multiselect = true; choofdlog.ShowDialog(); } private void replacebtn_Click(object sender, EventArgs e) { // This is where i'm having trouble } public static void ReplaceFile(string FileToMoveAndDelete, string FileToReplace, string BackupOfFileToReplace) { File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false); } } 解决方案 For OpenFileDialog:OpenFileDialog choofdlog = new OpenFileDialog();choofdlog.Filter = "All Files (*.*)|*.*";choofdlog.FilterIndex = 1;choofdlog.Multiselect = true;if (choofdlog.ShowDialog() == DialogResult.OK){ string sFileName = choofdlog.FileName; string[] arrAllFiles = choofdlog.FileNames; //used when Multiselect = true}For FolderBrowserDialog:FolderBrowserDialog fbd = new FolderBrowserDialog();fbd.Description = "Custom Description";if (fbd.ShowDialog() == DialogResult.OK){ string sSelectedPath = fbd.SelectedPath;}To access selected folder and selected file name you can declare both string at class level.namespace filereplacer{ public partial class Form1 : Form { string sSelectedFile; string sSelectedFolder; public Form1() { InitializeComponent(); } private void direc_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); //fbd.Description = "Custom Description"; //not mandatory if (fbd.ShowDialog() == DialogResult.OK) sSelectedFolder = fbd.SelectedPath; else sSelectedFolder = string.Empty; } private void choof_Click(object sender, EventArgs e) { OpenFileDialog choofdlog = new OpenFileDialog(); choofdlog.Filter = "All Files (*.*)|*.*"; choofdlog.FilterIndex = 1; choofdlog.Multiselect = true; if (choofdlog.ShowDialog() == DialogResult.OK) sSelectedFile = choofdlog.FileName; else sSelectedFile = string.Empty; } private void replacebtn_Click(object sender, EventArgs e) { if(sSelectedFolder != string.Empty && sSelectedFile != string.Empty) { //use selected folder path and file path } } ....}NOTE:As you have kept choofdlog.Multiselect=true;, that means in the OpenFileDialog() you are able to select multiple files (by pressing ctrl key and left mouse click for selection).In that case you could get all selected files in string[]:At Class Level:string[] arrAllFiles;Locate this line (when Multiselect=true this line gives first file only):sSelectedFile = choofdlog.FileName;To get all files use this:arrAllFiles = choofdlog.FileNames; //this line gives array of all selected files 这篇关于如何从打开文件对话框,并获得的FolderBrowserDialog文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-08 09:01