简体   繁体   English

如何从服务器读取多个文件到c#

[英]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: 那么问题是:

  1. How would I approach this? 我该如何处理?
  2. How exactly should I get the number of files? 我究竟应该如何获得文件数量?
    (I'm guessing I'd have to read the server file which contains them.) (我猜我必须读取包含它们的服务器文件。)

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.

相关问题 如何从C#的文件夹中读取多个.CSV文件? - How to read multiple .CSV files from a folder in c#? 如何从c#中的多个应用程序配置文件读取值? - How to read values from multiple application configuration files in c#? 如何从URL从Windows应用程序读取大量xml文件(从Windows应用程序向服务器发送多个请求)C# - How to read numerous xml files from windows appication from URL (multiple requests to server from windows app) c# 如何读取多个文件txt c# - how to read multiple files txt c# c#如何从1个文件中搜索后从多个文件中读取同一行? - c# how to read same line from multiple files after a search from 1 of the files? 从64位版本服务器中的C#中读取Excel文件 - Read Excel files from C# in 64 bit version server 如何在1个消息框C#中读取和显示多个文本文件的信息? - How can i read and display info from multiple text files in 1 message box C#? 异步C#服务器从多个套接字连续读取 - Asynchronous C# Server Continuously read from multiple sockets C#Server不会读取客户端发送的多行 - C# Server will not read in multiple lines sent from the client 如何在c#中的列表中读取多个csv文件的内容 - How to read the content of multiple csv files in a list in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM