如何在C#中显示Filtered OpenFileDialogeBox? [英] How to display a Filtered OpenFileDialogeBox in C#?

查看:82
本文介绍了如何在C#中显示Filtered OpenFileDialogeBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends,



我在C#.net中显示openfileDialog时遇到问题。

在Openfiledialog中现在显示我的系统中的所有驱动器,如C:,D:,E :,网络,MyComputer等等,从中选择我需要的文件夹路径。



但我是什么需要我,我只需要在我的OpenFileDialog中显示网络驱动器文件夹

所有其他驱动器文件夹都不应该显示。



所以我没有找到一个方法来做它。



请帮助我用一些方法来完成这个过滤

Hello Friends,

I am facing a problem with displaying the openfileDialog in C#.net.
In the Openfiledialog is now displaying all the drives in my system like C:, D:, E:, Network ,MyComputer and all, from which am selecting my required folder path.

But what i need i , I Need to display only the Network Drive folder only in my OpenFileDialog
All other drive folders should not get displayed.

So far i havent found a methos to do it.

Please help me out withsome methods to make this Filtering to be done

推荐答案

我同意Manas ..我不认为有任何方法可以过滤驱动器:但你可以按照所示的方式进行操作:



OpenFileDialog ofd = new OpenFileDialog();

ofd.CustomPlaces.Clear();

foreach(System.IO.DriveInfo.GetDrives()中的var项目)

{

if(item.DriveType == DriveType.Removable)

ofd.CustomPlaces.Add(item.RootDirectory.ToString());

}



if(ofd.ShowDialog()== DialogResult.OK)

{

FileInfo f = new FileInfo (ofd.FileName);

string s = f.Directory.Root.ToString();

DriveInfo df = new DriveInfo;

if(df.DriveType == DriveType.Removable)

{

//带文件的STUFF

}

}
i am agree with Manas .. i dont think there is any way to filter with drives: but u can to following stuff as shown:

OpenFileDialog ofd = new OpenFileDialog();
ofd.CustomPlaces.Clear();
foreach (var item in System.IO.DriveInfo.GetDrives())
{
if (item.DriveType == DriveType.Removable)
ofd.CustomPlaces.Add(item.RootDirectory.ToString());
}

if (ofd.ShowDialog() == DialogResult.OK)
{
FileInfo f = new FileInfo(ofd.FileName);
string s = f.Directory.Root.ToString();
DriveInfo df = new DriveInfo(s);
if (df.DriveType == DriveType.Removable)
{
//DO STUFF WITH FILE
}
}


我不认为有可能实现你想用OpenFileDialog做的事情。



您可以编写自己的自定义对话框,也可以在进行选择后进行验证。
I don't think that it is possible to to achieve what you are trying to do with OpenFileDialog.

You can either write your own custom dialog or do validation after you have made the selection.


使用substring函数获取路径的前两个字符,

如果路径以\\,它应该是一个以网络驱动器名称开头的路径。

否则,您可以切断前3个字符并添加驱动器名称。
Use substring function to get the first two characters of the path,
if the path start with \\, it should be a path start with network drive name.
Else, you can cut off the first 3 characters and add the drive name.


这篇关于如何在C#中显示Filtered OpenFileDialogeBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆