[英]Read Multiple Files StreamReader
I have read and researched this throughout the internet and hoping now someone can help me through this. 我已经在整个互联网上阅读并进行了研究,希望现在有人可以帮助我。 I am writing to read three different text files with different names, but I need to read through the text file name in order to determine which method I have to execute for that particular file.
我正在编写读取具有不同名称的三个不同的文本文件,但是我需要通读文本文件的名称,以确定要为该特定文件执行的方法。 This is what I have so far, any help would be greatly appreciated.
这是我到目前为止所拥有的,任何帮助将不胜感激。
var readers = new List<StreamReader>();
foreach (var filename in names_of_files)
{
if (do something?)
{
switch
}
}
using (StreamReader file = new StreamReader(filename))
{
(reads the file and does other stuff)
}
Below is the method that I am using to select multiple files. 下面是我用来选择多个文件的方法。
private void LoadNewFile()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
System.Windows.Forms.DialogResult dr = ofd.ShowDialog();
ofd.Filter = "Text Files(*.txt) | *.txt";
foreach (String file in ofd.FileNames)
{
if (dr == DialogResult.OK)
{
userSelectedFilePath += file;
names_of_files.Add(userSelectedFilePath);
}
}
}
You can loop through the file names and process it differently based on the filename. 您可以循环浏览文件名,并根据文件名进行不同的处理。
foreach (var filename in names_of_files)
{
using (StreamReader file = new StreamReader(filename))
{
if (filename.Contains(".txt"))
{
// Process text tile
MyTextProcessingMethod(file);
}
else if (filename.Contains(".png"))
{
// Do something with the image
MyImageProcessingMethod(file);
}
else if (filename.Contains("_specialCode")) {
// Another file that has special processing based on its file name
MySpecialProcessingMethod(file);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.