返回zip文件夹和文件名时,C#字符已更改

时间:2016-07-28 13:04:50

标签: c# zip

我正在编写一个循环一系列日期的脚本,检查每个日期是否为有效交易日,并检查每个日期是否有相应的文件夹和文件名。

Quantconnect/Lean Githib repo for classes

我遇到的问题是我的File.Exists(dataFile)方法每次都会出错。 原因似乎是formattedFolderName中存储的文件夹名称的字符已更改。

目标文件夹本身是压缩的,所以我想知道这可能是问题吗?

预期的文件路径:C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\audjpy\20070401_quote\20070401_audjpy_minute_quote

实际文件路径:C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\audjpy\20070401_quoAe\20070401_au1jp7_0inuAe_quoAe

我认为它可能是zip文件夹,20070401_quote弄乱了这个,但我不确定。

Program.cs

using System;
using System.IO;
using QuantConnect;
using QuantConnect.Securities;
using QuantConnect.Securities.Forex;

namespace TradingDaysFileChecker
{
    class Program
    {
        static void Main(string[] args)
        {
            var startDate = new DateTime(2007, 04, 01);
            var endDate = new DateTime(2016, 07, 25);
            var dataFilePath = @"C:\Users\richa_000\Desktop\exporter\forex\fxcm\minute\";

            var securityType = SecurityType.Forex;
            var ticker = TickType.Trade;
            var marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
            var market = Market.FXCM;
            var symbol = Symbol.Create(ticker.ToString(), securityType, market);
            var marketHoursDbEntry = marketHoursDatabase.GetEntry(symbol.ID.Market, symbol.Value, symbol.ID.SecurityType);

            var exchange = new ForexExchange(marketHoursDbEntry.ExchangeHours);
            var validTradingDays = new DateHandler(startDate, endDate, dataFilePath).IterateOverDateRange(exchange);

            var forexSecuritiesFolders = Directory.GetDirectories(dataFilePath);

                foreach (var validDay in validTradingDays)
                {
                    foreach (var forexSecurity in forexSecuritiesFolders)
                    {
                        var securityName = new DirectoryInfo(forexSecurity).Name.ToString();
                        var formattedFolderName = validDay.ToString("yyyyMMdd_quote");
                        var formattedFileName = validDay.ToString($"yyyyMMdd_{securityName}_minute_quote");

                        var dataFile = dataFilePath + securityName + @"\" + formattedFolderName + @"\" + formattedFileName;

                        if (!File.Exists(dataFile))
                        {
                            Console.WriteLine(dataFile);
                            Console.ReadLine(); // Stopping after the first run while troubleshooting
                        }
                    }
                }
                Console.ReadLine();
        }
    }
}

DateHandler.cs

using System;
using System.Collections.Generic;
using QuantConnect.Securities.Forex;

namespace TradingDaysFileChecker
{
    public class DateHandler
    {
        private DateTime _startDate;
        private DateTime _endDate;

        public DateHandler(DateTime startDate, DateTime endDate, string filePath)
        {
            _startDate = startDate;
            _endDate = endDate;
        }

        public IEnumerable<DateTime> IterateOverDateRange(ForexExchange exchange)
        {
            for (var day = _startDate.Date; day.Date <= _endDate.Date; day = day.AddDays(1))
                if (exchange.IsOpenDuringBar(day.Date, day.Date.AddDays(1), false))
                {
                    yield return day;
                }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

好的,您是如何从DateTime对象构建格式化名称的。当你调用.ToString()时,它会查找特殊的格式化字符,因此你不能在任何文字文本中包含任何字符。你的&#39;正在转换为A,因为&#39; t&#39;是经络的格式化字符(上午/下午,但只有一个&#39; t只给你A或P)和“&#39; m&#39;同样的东西,它被替换为该DateTime对象的分钟的一位数。

试试这个:

                var formattedFolderName = validDay.ToString("yyyyMMdd") + "_quote";
                var formattedFileName = validDay.ToString($"yyyyMMdd") + securityName + "_minute_quote";