c#用相同的方法选择文件夹或文件
本文关键字:选择 文件夹 文件 方法 | 更新日期: 2024-08-09 23:45:52
好吧,所以请不要太激动我,这是我在这里的第一个问题,也许我想做的甚至都不可能。显然我不是专家;这就是我来找你的原因
我搜索了这里、MSDN和互联网的其他部分(大部分都指向这里),但没有任何运气。我确实看到一个关于使用OpenFileDialog
选择文件夹而不是文件的问题。我几乎可以肯定,我在主流应用程序中看到过这一点,但这个问题被标记为过于模糊,而且在回复中没有解决这个特别的警告。
我有一些文本框需要文件/文件夹路径。我想将处理此问题的两种方法简化为一种。唯一的区别是,一次选择文件,另一次选择文件夹。为了简单易读,我想将它们合并。
如果不将每个代码方法的内容放入一个大的IF
中,这可能吗?
以下是两种方法:
private void FolderBrowser(object sender, EventArgs e)
{
TextBox SenderBox = sender as TextBox;
if (SenderBox.Text != "")//if the text box is not empty
{
//set the selected path to the text box's current contents (incase of accidental entry)
FileBrowserDialog.FileName = SenderBox.Text;
}
if (FileBrowserDialog.ShowDialog() == DialogResult.OK)
{
SenderBox.Text = FileBrowserDialog.FileName;
}
}
private void FileBrowser(object sender, EventArgs e)
{ //basically the same as the folder browser above, but for selecting specific files
TextBox SenderBox = sender as TextBox;
if (SenderBox.Text != "")//if the text box is not empty
{
//set the selected path to the text box's current contents (incase of accidental entry)
FileBrowserDialog.FileName = SenderBox.Text;
}
if (FileBrowserDialog.ShowDialog() == DialogResult.OK)
{
SenderBox.Text = FileBrowserDialog.FileName;
}
}
我为每个TextBox
添加了一个Tag
,表示它需要一个文件还是一个文件夹。我想使用Tag
作为条件,通过它来确定我是否应该使用文件或文件夹浏览器。这就是我无知的表现;我曾设想过这样一个非工作代码:
private void browser(object sender, EventArgs e)
{
//cast sender as a textbox
TextBox tBox = (TextBox)sender;
object browser = null;
if (tBox.Tag.ToString().Equals("Folder"))
{
browser = new FolderBrowserDialog();
}
else
{
browser = new OpenFileDialog();
}
if (tBox.Text != "")//if the text box is not empty
{
//set the selected path to the text box's current contents (incase of accidental entry)
browser.FileName = tBox.Text;
}
if (browser.ShowDialog() == DialogResult.OK)
{
tBox.Text = browser.FileName;
}
}
我疯了吗,或者有办法实现我心中的想法吗?需要明确的是,我想知道是否有:
- 允许选择文件或文件夹的现有对象/方法,或
- 一种将对象动态重新定义为不同类型对象的方法
- 使用1方法的任何其他方式,以动态地允许基于在调用对象上定义的某些
Tag
来使用OpenFileDialog
或FileBrowserDialog
这是我发现的在不依赖第三方代码的情况下解决此问题的最简单方法,但您需要添加一些健全性检查,以防用户在输入时出错:
OpenFileDialog ofd = new OpenFileDialog();
ofd.CheckFileExists = false;
string defaultFilename = "Select this folder";
ofd.FileName = defaultFilename;
if (ofd.ShowDialog().Value)
{
// Check if the user picked a file or a directory, for example:
if (!ofd.FileName.Contains(defaultFilename))
{
// File code
}
else // You should probably turn this into an else if instead
{
// Directory code
}
// Alternatively, but still as unsafe
if (File.Exists(ofd.FileName))
{
// File code
}
else
{
// Directory code
}
}
基本上,这里的"窍门"是将OpenFileDialog的CheckFileExists设置为false。
尝试使用FolderBrowserDialogEx。
请在此处查看详细答案:如何配置OpenFileDialog来选择文件夹?
两个对话框(FileOpenDialog
和FolderBrowserDialog
)都继承自CommonDialog
;但是,这个基类没有用于检索结果的属性。此外,属性在两个对话框中的命名也不同。
你可以通过创建一个包装来解决这个问题。继承是将对象重新定义为不同类型的正确方法。
public abstract class FileFolderDialogBase
{
public abstract bool ShowDialog();
public string Result { get; protected set; }
}
public class FileDialog : FileFolderDialogBase
{
public override bool ShowDialog()
{
var ofd = new OpenFileDialog();
if ofd.ShowDialog() == DialogResult.OK) {
Result = ofd.FileName;
return true;
}
return false;
}
}
public class FolderDialog : FileFolderDialogBase
{
public override bool ShowDialog()
{
var fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
Result = fbd.SelectedPath;
return true;
}
return false;
}
}
用法:
var dialog = textBox.Tag == "Folder" ? new FolderDialog() : new FileDialog;
if (dialog.ShowDialog()) {
textBox.Text = dialog.Result;
}
你可以通过创建一个工厂级来进一步推动它
public static class FileFolderDialog
{
public static FileFolderDialogBase Create(string type)
{
swich (type.ToLowerInvariant()) {
case "folder":
case "dir":
case "directory":
return new FolderDialog();
default:
return new FileDialog();
}
}
}
使用
var dialog = FileFolderDialog.Create(textBox.Tag);
if (dialog.ShowDialog()) {
textBox.Text = dialog.Result;
}
为什么不尝试扩展TextBox类?它简单且可重复使用,您只需要将自定义控件拖放到WinForm
class FileTextBox : System.Windows.Form.TextBox{
//===>This enumeration is more readable insted of a string XD
public enum DialogType{
File,Folder
}
//===>This property we will handle what kind of Dialog to show
public DialogType OpenDialogType{
get;
set;
}
//===>This is where Object Oriented Programming a& Design do his magic
public System.Windows.Forms.DialogResult ShowDialog(string Title =""){
//===>This function is where we define what kind of dialog to show
System.Windows.Forms.DialogResult Result = System.Windows.Forms.DialogResult.None ;
object Browser=null;
switch(this.OpenDialogType){
case DialogType.File:
Browser = new OpenFileDialog();
((Browser)OpenFileDialog).Title= Title;
if(this.Text.Trim() !="" && this.Text != null ){
((Browser)OpenFileDialog).FileName = this.Tex;
}
Result = ((Browser)OpenFileDialog).ShowDialog();
break;
case DialogType.Folder:
Browser = new FolderBrowserDialog ();
((Browser)FolderBrowserDialog).Description = Title;
if(this.Text.Trim() !="" && this.Text != null ){
((Browser)FolderBrowserDialog).RootFolder = this.Text;
}
Result = ((Browser)FolderBrowserDialog).ShowDialog();
break;
}
return Result;//===>We return thi dialog result just if we want to do something else
}
}
/*
Create a class and copy/paste this code, I think is going to work because
I didn't compiled then go to ToolBox window find this control and Drag & Drop
to your WinForm and in the property window find OpenDialogType property
and this is where you kind define the behavior of OpenDialog();
I'm currently working in a little project in Vs where I create a custom
UI Control downloaded from my git repository
https://github.com/MrAlex6204/GYMSystem
*/