问候。我是编码的新手,所以对于某些人来说,我面临的这些问题可能很容易。我正在尝试制作一个简单的工具,以导出文件并读取文件中的价格数据。但是首先我需要能够导入\ 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();
}
}
}
答案 0 :(得分: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();
//............
});