[英]How to read multiple files from server into c#
I want to know how to read multiple (about 500-1000) text files which are located on a server. 我想知道如何读取位于服务器上的多个(大约500-1000)个文本文件。 So far, I've written code for a program that only reads a single text file.
到目前为止,我已经为只读取单个文本文件的程序编写了代码。
Here's how I'm currently reading a single file. 这是我目前正在阅读单个文件的方式。
public void button1_Click(object sender, EventArgs e)
{
// Reading/Inputing column values
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] fileLines = File.ReadAllLines(ofd.FileName);
I would like to get rid of the open file dialog box, and let the program automatically read the 500-1000 text files where are located in the sever. 我想摆脱打开文件对话框,让程序自动读取位于服务器中的500-1000文本文件。
I'm thinking something along the lines of 我正在思考一些事情
for (int i =0; i<numFiles; i++)
{
//just use string[] fileLines =File.ReadAllLines()
//how would i specify the path for multiple files?
}
Questions are then: 那么问题是:
You can use Directory.GetFiles(string path) to get all files from a certain directory. 您可以使用Directory.GetFiles(字符串路径)从特定目录中获取所有文件。 You can then use a foreach loop to iterate through all the files in that directory and do your processing.
然后,您可以使用foreach循环遍历该目录中的所有文件并进行处理。
How you go about getting your files depends on if they are all located in the same directory of if you'll need to recursively search through a directory and all child directories. 如何获取文件取决于它们是否都位于同一目录中,如果您需要递归搜索目录和所有子目录。 Directory.GetFiles is where you want to start.
Directory.GetFiles是您要开始的地方。 It has 3 overloads seen here .
它有3个可见重载这里 。 So you might try something like this:
所以你可能会尝试这样的事情:
string path = "\mypath\tosomehwere";
string searchPattern = "*.txt";
string[] MyFiles = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
Then just loop through the string array and proccess each file as you would normally. 然后只需循环遍历字符串数组并按正常方式处理每个文件。
foreach (string filePath in MyFiles)
{
MyFileProcessMethod(filePath)
}
Path.GetFileName(filePath)
will return the individual text file name should you need it for your processing requirements. Path.GetFileName(filePath)
将根据您的处理要求返回单个文本文件名。
You can use recursion to loop through all directories. 您可以使用递归遍历所有目录。 Using Directory.EnumerateFiles also allows you to use a
foreach
loop so you don't have to worry about the file count. 使用Directory.EnumerateFiles还允许您使用
foreach
循环,因此您不必担心文件计数。
private static void ReadAllFilesStartingFromDirectory(string topLevelDirectory)
{
const string searchPattern = "*.txt";
var subDirectories = Directory.EnumerateDirectories(topLevelDirectory);
var filesInDirectory = Directory.EnumerateFiles(topLevelDirectory, searchPattern);
foreach (var subDirectory in subDirectories)
{
ReadAllFilesStartingFromDirectory(subDirectory);//recursion
}
IterateFiles(filesInDirectory, topLevelDirectory);
}
private static void IterateFiles(IEnumerable<string> files, string directory)
{
foreach (var file in files)
{
Console.WriteLine("{0}", Path.Combine(directory, file));//for verification
try
{
string[] lines = File.ReadAllLines(file);
foreach (var line in lines)
{
//Console.WriteLine(line);
}
}
catch (IOException ex)
{
//Handle File may be in use...
}
}
}
Also note Directory.EnumerateFiles provides overload that lets you provide a search pattern to narrow the scope of the files. 另请注意, Directory.EnumerateFiles提供重载,允许您提供搜索模式以缩小文件范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.