How to get file path from OpenFileDialog and FolderBrowserDialog?
嘿,几天前我开始学习C,我正在尝试制作一个程序,将文件复制并粘贴到选定的目录(如果需要,还可以替换),但我不知道如何从OpenFileDialog和FolderBrowserDialog中获取目录和文件路径。
我做错什么了?
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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); } } |
对于OpenFileDialog:
1 2 3 4 5 6 7 8 9 10 | 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 } |
对于folderbrowserdialog:
1 2 3 4 5 6 7 | FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.Description ="Custom Description"; if (fbd.ShowDialog() == DialogResult.OK) { string sSelectedPath = fbd.SelectedPath; } |
要访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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 } } .... } |
注:
由于您保留了
在这种情况下,您可以在
班级级别:
1 | string[] arrAllFiles; |
定位此行(当
1 | sSelectedFile = choofdlog.FileName; |
要获取所有文件,请使用:
1 | arrAllFiles = choofdlog.FileNames; //this line gives array of all selected files |
您可以将路径存储到字符串变量中,例如
1 | string s = choofdlog.FileName; |
使用
用法很简单。
1 | string directoryPath = System.IO.Path.GetDirectoryName(choofdlog.FileName); |
要获取选定文件的完整文件路径,则需要对一个文件使用文件名属性,对多个文件使用文件名属性。
1 | var file = choofdlog.FileName; // for one file |
或多个文件
1 | var files = choofdlog.FileNames; // for multiple files. |
要获取文件的目录,可以使用path.getdirectoryname下面是乔恩·基特对从路径获取目录的类似问题的回答。
有效的原始快速修复。
如果只使用
1 2 3 | exampleFileName = ofd.SafeFileName; exampleFileNameFull = ofd.FileName; exampleFileNameFolder = ofd.FileNameFull.Replace(ofd.FileName,""); |
将此类创建为扩展名:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public static class Extensiones { public static string FolderName(this OpenFileDialog ofd) { string resp =""; resp = ofd.FileName.Substring(0, 3); var final = ofd.FileName.Substring(3); var info = final.Split('\'); for (int i = 0; i < info.Length - 1; i++) { resp += info[i] +"\"; } return resp; } } |
然后,您可以这样使用:
1 2 3 4 5 | //ofdSource is an OpenFileDialog if (ofdSource.ShowDialog(this) == DialogResult.OK) { MessageBox.Show(ofdSource.FolderName()); } |
在
如果我迟了答复,我很抱歉,但我只是认为我应该为OpenDialog提供一个更简单的解决方案。
1 2 3 | OpenDialog ofd = new OpenDialog(); var fullPathIncludingFileName = ofd.Filename; //returns the full path including the filename var fullPathExcludingFileName = ofd.Filename.Replace(ofd.SafeFileName,"");//will remove the filename from the full path |
我以前还没有用过folderbrowserdialog,所以我相信我的同事们会接受这一点。我希望这有帮助。