如何在单击“浏览”按钮时一次选择多个文本文件(.txt)? [英] How to select multiple textfiles (.txt) at a time on clicking Browse button ?

查看:86
本文介绍了如何在单击“浏览”按钮时一次选择多个文本文件(.txt)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



如果不明白请再次询问



请...



我正在使用MultiThread Windows应用程序c#



浏览按钮上的OpenFileDialog单击。 [选择并打开多个.txt文件]

Hi

if not understood please ask again

please ...

I am working on MultiThread Windows Application c#

OpenFileDialog on Browse button click. [select and open multiple .txt Files]

推荐答案

通过在选择文件时按住SHIFT或CTRL键,可以在OpenFileDialog框中选择多个txt文件。这将返回包含文件路径的数组。然后,您可以将每个路径分配给分配给线程的Open进程,
You can select multiple txt files in the OpenFileDialog box by holding down SHIFT or CTRL keys while selecting the files. This will return an array with the file paths. You can then assign each path to an Open process assigned to a thread,


另一个解决方案:



OpenFileDialog1.Multiselect = true ;





示例: -

流myStream = null;

OpenFileDialog Dialog = new OpenFileDialog();

Dialog.Title =Open Text File;

Dialog.Filter =TXT files | *。 txt;

Dialog.Multiselect = true; //允许在openfiledialog中选择多个文件

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^

// Dialog.InitialDirectory = @C:\;

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

{

尝试

{

if((myStream = Dialog.OpenFile() )!= null)

{

使用(myStream)

{



string [] filePaths = Directory.GetFiles(@D:\FolderName,* .txt,SearchOption.AllDirectories);

foreach(filePaths中的字符串文件)

{

rtxtFile.AppendText(System.IO.File.ReadAllText(file));

Thread.Sleep(1000);

}

}

}

}

catch(例外情况)

{

MessageBox.Show(错误:无法从磁盘读取文件。原始错误:+ ex.Message);

}

}
Another Solution :

OpenFileDialog1.Multiselect = true;


Example:-
Stream myStream = null;
OpenFileDialog Dialog = new OpenFileDialog();
Dialog.Title = "Open Text File";
Dialog.Filter = "TXT files|*.txt";
Dialog.Multiselect = true; //allows to select multiple files in openfiledialog
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Dialog.InitialDirectory = @"C:\";
if (Dialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = Dialog.OpenFile()) != null)
{
using (myStream)
{

string[] filePaths = Directory.GetFiles(@"D:\FolderName", "*.txt", SearchOption.AllDirectories);
foreach (string file in filePaths)
{
rtxtFile.AppendText(System.IO.File.ReadAllText(file));
Thread.Sleep(1000);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}


这篇关于如何在单击“浏览”按钮时一次选择多个文本文件(.txt)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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