本文整理汇总了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;
}
示例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");
}
}
示例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;
}
示例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");
}
}
示例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;
}
}
}