簡體   English   中英

嘗試寫入文件C#時出現System.FormatException

[英]System.FormatException when trying to write to file C#

我有一個簡單的程序,該程序讀取文本文件中的一行,將內容解析為字典,然后將該內容寫入文件。

文本文件中的上述行是:

1433 =成員; 1064 = EndOfDayReportGeneration; 5679 = 1; 5678 = 22; 71 = 200602260億; 3141 = ClrngETL; 72 = 20100301092024000; 2144 = CH004CR; 4059 = 20060226; 1828 = 20060226; 1823 = 20100301; 969 = 0048; 10003 = ACME CIMENT INC。; 1180 =巡回賽 ACCECIMENT 222 FRESNO COLLEGE AVE BUR 500,CA,MQ,H82 2P3; 1054 = true; 1055 = 5143502945; 1059 = JENNIFER ALLEY; 1058 = 5143502945; 1057 = TIM BROWN; 1056 = 5143502865; 1088 = LORD; 1089 = 12; 1090 = 5143502855; 773 =光學,幻覺; 1144 = 0000; 1195 = 1; 955 = ClientAccountProfile; 956 = ClearingMember; 1558 = 10000; 583 = {17 | 100}; 6385 = 20081208; 5049 = PPCD; 5578 = GA; 5579 = 000; 6143 = 20060226; 70 = 000000000; 73 = 20100301; 80 = 092024000; 2578 = 98138000

我得到了System.FormatException:嘗試寫入文件時,索引(從零開始)必須大於或等於零且小於參數列表的大小。

這是我的代碼:

class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, string> dicParsed = new Dictionary<string, string>();

        try
        {
            StreamReader sr = new StreamReader("testFormat.txt");
            String line;
            line = sr.ReadLine();

            while (line != null)
            {
                dicParsed = line.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                 .Select(part => part.Split('='))
                 .ToDictionary(split => split[0], split => split[1]);
                string[] data = line.Split(null);

                line = sr.ReadLine();
            }

            //close the file
            if (sr != null)
            {
                sr.Close();
            }

        }
        catch (Exception except)
        {
            Console.WriteLine("Exception: " + except.Message);
        }
        finally
        {
            Console.WriteLine("Configuration File Parsed");
        }

        using (StreamWriter swLog = new StreamWriter("testLog.txt", true))
        {

            StringBuilder message = new StringBuilder("TEST "); 
            message.Append(Environment.NewLine);
            foreach (KeyValuePair<string, string> paires in dicParsed)
            {                    
                    message.Append(paires.Key.ToString() + "=" + paires.Value.ToString() + ";");
                    Console.WriteLine();
            }

            try
            {
                string test = message.ToString();
                swLog.Write(test, true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {

            }
        }
    }
} // end class program

您正在調用StreamWriter.Write(test, true) 我不知道為什么 這將調用StreamWriter.Write(string, object)重載,該重載用於格式化打印字符串(例如Console.WriteLine("Hello, {0}", name) )。

您的test字符串包含{17|100}而不是{0} ,因此它會拋出。 只需使用Write(string)重載即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM