c#以相同方法选择文件夹或文件(c# select Folders or Files in the same method)

编程入门 行业动态 更新时间:2024-10-28 06:22:23
c#以相同方法选择文件夹或文件(c# select Folders or Files in the same method)

好的,所以请不要太激怒我,这是我的第一个问题,也许我想做的事情甚至不可能。 显然我不是专家; 这就是我要来找你的原因。 :)

我在这里搜索过,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方法动态允许使用OpenFileDialog或FileBrowserDialog基于调用对象上定义的某些Tag 。

Okay, so please don't flame me too much, this is my 1st question here, and maybe what I am trying to do is not even possible. Obviously I am not an expert; that's why I am coming to you. :)

I have searched all over here, MSDN, and the rest of the internet (most of which points back here) without any luck. I did see one question asking about using the OpenFileDialog to select a folder instead of a file. I am almost certain that I have seen this in mainstream applications, but the question was marked as being too vague, and that particular caveat was unaddressed in the responses.

I have some text boxes that need file/folder paths. I want to simplify the two methods that handle this, into one. The only difference, is that once selects a file, and the other selects a folder. For simplicity and readability, I'd like to consolidate them.

Is this possible, without literally putting the contents of each code method into a big IF ?

Here are the two methods:

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; } }

I have added a Tag to each TextBox, indicating if it needs a file or a folder. I'd like to use the Tag as the condition by which I determine if I should be using a file or folder browser. This is where my ignorance shows; I had envisioned something like this NONWORKING code:

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; } }

Am I crazy, or is there a way to accomplish what I have in mind? To be clear, I want to know if there is:

An existing Object/Method that would allow for the selection of a file or a folder, or A way to dynamically re-define an object as a different type of object Any other way to use 1 Method to dynamically allow for the use of OpenFileDialog or FileBrowserDialog based on some Tag defined on the calling object.

最满意答案

这是我发现在不依赖第三方代码的情况下解决此问题的最简单方法,但是您需要添加一些健全性检查,以防用户使用输入:

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。

This is the easiest way I've found of solving this problem without relying on third party code, but you'll need to add some sanity checks, in case the user goofs around with the input:

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 } }

Basically, the "trick" here is to set OpenFileDialog's CheckFileExists to false.

更多推荐

本文发布于:2023-08-04 12:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416490.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   文件   方法   select   Files

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!