0

问候。我是编码新手,所以我面临的这些问题对你们中的一些人来说可能很容易。我正在尝试制作一个简单的工具来获取导出的文件并读取文件中的价格数据。但首先我需要能够导入当前位于 \Marketlogs 中的文件。所有文件都是.txt。我已经让它使用一个集合名称,但我还没有找到导入未知名称的方法。这是一个文件名示例,它可能会提取“Esoteria-Liquid Ozone-2019.05.04 015804”

我一直在关注 IAmTimCorey youtube 视频,了解如何阅读文件的基本想法,但他为他的视频使用了固定文件名

https://www.youtube.com/watch?v=cST5TT3OFyg&list=PLsBhi5lzz1f2-AbhurnGazN68UgG4dVwt&index=1

https://www.youtube.com/watch?v=yClSNQdVD7g&list=PLsBhi5lzz1f2-AbhurnGazN68UgG4dVwt&index=2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;


namespace Eve_market
{
    class Program
    {
        static void Main(string[] args)
        {
            /// MarketLog
            // opens a file from MarketLogs 
         ```   string filePath = @"E:\Users\Hamilton Norris\Documents\EVE\logs\Marketlogs\"; ```
            // converts file to a  readable csv from text with filteable deta
            List<MarketLog> log = new List<MarketLog>();

            List<string> lines = File.ReadAllLines(filePath).ToList();

            foreach (string line in lines)
            {
                string[] enteries = line.Split(',');

                MarketLog newMarketLog = new MarketLog();

                newMarketLog.Price = enteries[0];
                newMarketLog.VolReamining = enteries[1];
                newMarketLog.TypeID = enteries[2];
                newMarketLog.Range = enteries[3];
                newMarketLog.OrderID = enteries[4];
                newMarketLog.VolEntered = enteries[5];
                newMarketLog.MinVolume = enteries[6];
                newMarketLog.Bid = enteries[7]; // False = sell order  true = buy order
                newMarketLog.IssueDate = enteries[8];
                newMarketLog.Bidurationd = enteries[9];
                newMarketLog.StationID = enteries[10];
                newMarketLog.RegionID = enteries[11];
                newMarketLog.SolarSystemID = enteries[12];
                newMarketLog.Jumps = enteries[13]; //  0 = same system

                log.Add(newMarketLog);
            }

            foreach (var MarketLog in log)
            {
                Console.WriteLine($"{ MarketLog.Price } {MarketLog.VolReamining } {MarketLog.Bid} ");

            }


            // MarketLog End

            Console.ReadLine();
        }


    }
}
4

1 回答 1

0

请尝试以下代码:(获取目录,然后获取具有匹配文件模式的文件。然后一一加载并像往常一样使用您的代码读取文件)

 var fileNamePattern = "*.txt";
 var dir = new DirectoryInfo(path);
 IEnumerable<FileSystemInfo> files = dir.GetFileSystemInfos(fileNamePattern);
 files
    .ToList()
    .ForEach(f=>        
    {
       //Load each file using File system Info:
       List<string> lines = File.ReadAllLines(f.FullName).ToList();
       //............
    });
于 2019-05-06T04:49:44.920 回答