如何在C#中一次浏览文件和文件夹 [英] how to browse files as well as folders at a time in C #
问题描述
如何一次浏览文件夹和文件?
如果我使用openFileDialog,它只能浏览文件
如果我使用openFolderdialog,则只能浏览文件夹.
请向我建议如何做或提供一些示例代码.
.....
嗨..
我有一个浏览按钮和一个文本框.在browser_button click事件中,我想浏览文件并将路径名放入文本框.为此,我使用Openfile对话框编写了这样的代码.
Hi,
How can I browse both folders and files at a time?
If I use openFileDialog it can browse only files
If I use openFolderdialog it can browse folders only.
Please suggest to me how to do it or give some sample code.
.....
hi..
I have one browse button and one text box. In the browse_button click event, I would like to browse the files and place the path name into textbox. For this i''vw written code like this by using openfile dialog.
private void brwsbtn_Click(object sender, EventArgs e)
{
if (openFD.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFD.FileName;
}
textBox1.Text="";
}
这样我就只能选择文件.
So that i am able to select files only. how can i select and place the folders path in textbox.
推荐答案
new System.IO.FileInfo(openFD.FileName).Directory.ToString()
将只为您提供文件的路径.
您是否正在寻找类似的东西?
new System.IO.FileInfo(openFD.FileName).Directory.ToString()
will give you only the path of the file.
Are you loking out for something like that?
//我有一个列出所有文件和文件夹的代码
字符串sourcePath = sourceFolderPath//输入您的文件夹名称
int numcount = 0;
int i = 0;
int f = 0;
foreach(Directory.GetFiles(sourcePath)中的字符串fl)
{
filelist [f] = fl;//父目录中的所有文件
f ++;
}
while(sourcePath!= null)
{
//递归获取所有子文件夹
foreach(Directory.GetDirectories(sourcePath)中的字符串drs)
{
//获取子目录中的所有文件
foreach(Directory.GetFiles(drs)中的字符串文件)
{
filelist [f] =文件;
f ++;
}
directoylist [i] = drs;//子目录
i ++;
}
sourcePath = directoylist [numcount];
numcount ++;
}
//i have a code to list all files and folders
string sourcePath = sourceFolderPath//enter your folder name
int numcount = 0;
int i = 0;
int f=0;
foreach (string fl in Directory.GetFiles(sourcePath))
{
filelist[f] = fl;//all files in parent directory
f++;
}
while (sourcePath != null)
{
//get all subfolders recursively
foreach (string drs in Directory.GetDirectories(sourcePath))
{
//get all files in subdirectories
foreach (string files in Directory.GetFiles(drs))
{
filelist[f] = files;
f++;
}
directoylist[i] = drs;//sub directories
i++;
}
sourcePath = directoylist[numcount];
numcount++;
}
这篇关于如何在C#中一次浏览文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!