当前位置: 首页>>代码示例>>C#>>正文


C# TextFieldParser.SetFieldWidths方法代码示例

本文整理汇总了C#中Microsoft.VisualBasic.FileIO.TextFieldParser.SetFieldWidths方法的典型用法代码示例。如果您正苦于以下问题:C# TextFieldParser.SetFieldWidths方法的具体用法?C# TextFieldParser.SetFieldWidths怎么用?C# TextFieldParser.SetFieldWidths使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.VisualBasic.FileIO.TextFieldParser的用法示例。


在下文中一共展示了TextFieldParser.SetFieldWidths方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateTextFieldParser

        public static TextFieldParser CreateTextFieldParser(string path, string extension)
        {
            TextFieldParser tfp;
            try
            {
                tfp = new TextFieldParser(path);
            }
            catch (FileNotFoundException e)
            {
                Console.Out.WriteLine(Properties.Settings.Default.fileOpenError);
                throw e;
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                switch (extension)
                {
                    case ".txt":
                        int[] format = { 3, 2, 10, 20, 20, 6, 6, 9 };
                        tfp.TextFieldType = FieldType.FixedWidth;
                        tfp.SetFieldWidths(format);
                        break;
                    case ".csv":
                        tfp.TextFieldType = FieldType.Delimited;
                        tfp.SetDelimiters(",");
                        break;
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(Properties.Settings.Default.fileFormatError);
                Console.Out.WriteLine(e);
            }

            return tfp;
        }
开发者ID:jkastl,项目名称:TaxReportKastl,代码行数:40,代码来源:TaxReportKastl.cs

示例2: SetFieldWidhtsTest

 public void SetFieldWidhtsTest()
 {
     using (StringReader reader = new StringReader ("abcd" + Constants.vbNewLine + "efgh" + Constants.vbNewLine))
     using (TextFieldParser t = new TextFieldParser (reader)) {
         t.TextFieldType = FieldType.Delimited;
         t.SetFieldWidths (new int [] { 1, 3, 2 });
         Assert.AreEqual ("1;3;2", Helper.Join (t.FieldWidths, ";"), "#01");
         Assert.AreEqual (FieldType.Delimited, t.TextFieldType, "#02");
     }
 }
开发者ID:h-endo12345,项目名称:mono-basic,代码行数:10,代码来源:TextFieldParserTest.cs

示例3: OpenTextFieldParser

 public static TextFieldParser OpenTextFieldParser(string file, params int[] fieldWidths)
 {
     TextFieldParser parser2 = new TextFieldParser(file);
     parser2.SetFieldWidths(fieldWidths);
     parser2.TextFieldType = FieldType.FixedWidth;
     return parser2;
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:7,代码来源:FileSystem.cs

示例4: FixedTest1

        public void FixedTest1()
        {
            using (StringReader reader = new StringReader ("abcdef" + Constants.vbNewLine + "1234" + Constants.vbNewLine + "ghijklmno" + Constants.vbNewLine))
            using (TextFieldParser t = new TextFieldParser (reader)) {
                t.SetFieldWidths (new int [] {1, 3, 2});
                t.TextFieldType = FieldType.FixedWidth;
                Assert.AreEqual ("a;bcd;ef", Strings.Join (t.ReadFields (), ";"), "#01");
                try {
                    Assert.AreEqual ("1;234", Strings.Join (t.ReadFields (), ";"), "#02");
                    Assert.Fail ("#E3 - Expected 'MalformedLineException'");
                } catch (MalformedLineException ex) {
                    Assert.AreEqual ("Line 2 cannot be parsed using the current FieldWidths.", ex.Message, "#E1");
                    Assert.AreEqual (2, ex.LineNumber, "#E2");
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#E4 - Expected 'MalformedLineException'");
                }
                Assert.AreEqual ("g;hij;kl", Strings.Join (t.ReadFields (), ";"), "#03");
            }

            using (StringReader reader = new StringReader ("abcdef" + Constants.vbNewLine + "1234" + Constants.vbNewLine + "ghijklmno" + Constants.vbNewLine))
            using (TextFieldParser t = new TextFieldParser (reader)) {
                t.SetFieldWidths (new int [] {});
                t.TextFieldType = FieldType.FixedWidth;
                try {
                    Assert.AreEqual ("a;bcd;ef", Strings.Join (t.ReadFields (), ";"), "#11");
                    Assert.Fail ("#E12 - Expected 'InvalidOperationException'");
                } catch (InvalidOperationException ex) {
                    Assert.AreEqual ("Unable to read fixed width fields because FieldWidths is Nothing or empty.", ex.Message, "#E11");
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#E13 - Expected 'InvalidOperationException'");
                }
            }

            using (StringReader reader = new StringReader (" bcdef" + Constants.vbNewLine + "1 234" + Constants.vbNewLine + "gh klmno" + Constants.vbNewLine))
            using (TextFieldParser t = new TextFieldParser (reader)) {
                t.SetFieldWidths (new int [] { 1, 3, 2 });
                t.TextFieldType = FieldType.FixedWidth;
                Assert.AreEqual (";bcd;ef", Strings.Join (t.ReadFields (), ";"), "#21");
                try {
                    Assert.AreEqual ("1;234", Strings.Join (t.ReadFields (), ";"), "#22");
                    Assert.Fail ("#E23 - Expected 'MalformedLineException'");
                } catch (MalformedLineException ex) {
                    Assert.AreEqual ("Line 2 cannot be parsed using the current FieldWidths.", ex.Message, "#E21");
                    Assert.AreEqual (2, ex.LineNumber, "#E22");
                } catch (Exception ex) {
                    Helper.RemoveWarning (ex);
                    Assert.Fail ("#E24 - Expected 'MalformedLineException'");
                }
                Assert.AreEqual ("g;h k;lm", Strings.Join (t.ReadFields (), ";"), "#23");
            }
        }
开发者ID:h-endo12345,项目名称:mono-basic,代码行数:53,代码来源:TextFieldParserTest.cs

示例5: _TextFieldParserFixedWidth

        /// <summary>
        /// Uses the Microsoft Text Field Parser to parse the FixedWidth file, to give a baseline
        /// against an established class library for parsing.
        /// </summary>
        private static void _TextFieldParserFixedWidth()
        {
            string[] fields;
            string s;

            using (VB.TextFieldParser tfp = new VB.TextFieldParser(PerformanceTests.FW_DATA_FILE))
            {
                tfp.TextFieldType = VB.FieldType.FixedWidth;
                tfp.SetFieldWidths(new int[PerformanceTests.NUMBER_OF_COLUMNS_IN_DATA] { 5, 5, 1, 28, 42, 15, 13, 9, 9, 1, 13, 14, 13, 6 });
                tfp.CommentTokens = new string[] { "#" };
                tfp.HasFieldsEnclosedInQuotes = true;

                while (!tfp.EndOfData)
                {
                    fields = tfp.ReadFields();

                    for (int i = 0; i < fields.Length; ++i)
                        s = fields[i] as string;
                }
            }
        }
开发者ID:chouzicz,项目名称:SQLForExcel,代码行数:25,代码来源:PerformanceTests.cs


注:本文中的Microsoft.VisualBasic.FileIO.TextFieldParser.SetFieldWidths方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。