C# 使用FileHelpers nuget包读取条形码
您好,我正在使用FileHelpers nugets库读取条形码。下面是一个例子:C# 使用FileHelpers nuget包读取条形码,c#,filehelpers,C#,Filehelpers,您好,我正在使用FileHelpers nugets库读取条形码。下面是一个例子: (3102)011780(10)772890 这是我的filehelpers类: class ReadBareCodes { [FieldFixedLength(6)] public string cbDate; [FieldFixedLength(6)] public double poidsNet; [FieldFixedLength(4)] publi
(3102)011780(10)772890
这是我的filehelpers类:
class ReadBareCodes
{
[FieldFixedLength(6)]
public string cbDate;
[FieldFixedLength(6)]
public double poidsNet;
[FieldFixedLength(4)]
public string dix;
[FieldFixedLength(6)]
public string lot;
}
字段poidsNet
始终有6个字符:011780
,但我希望它是这样的:0117,80
。下面是我尝试的:
private void ValidPdsNetBtn_Click(object sender, RoutedEventArgs e)
{
string bareCode = BareCode_TxtBox.Text;
string bareCodes_Path = @"O:\GT\GT9999 - Applications\G-Suite\Dossiers G-Suite\BareCodes_files\CB.txt";
using (StreamWriter sw = new StreamWriter(bareCodes_Path, append: false))
{
sw.Write(bareCode);
}
FixedFileEngine<ReadBareCodes> engine = new FixedFileEngine<ReadBareCodes>();
ReadBareCodes[] rst = engine.ReadFile(bareCodes_Path);
foreach(var det in rst)
{
Messages.InformationMessage($"Date : {det.cbDate}\nPoids Net : {det.poidsNet.ToString("0.00")}\n_Dix : {det.dix}\nLot : {det.lot}");
}
}
private void ValidPdsNetBtn\u单击(对象发送方,路由目标)
{
字符串bareCode=bareCode_TxtBox.Text;
字符串bareCodes_Path=@“O:\GT\GT9999-Applications\G-Suite\Dossiers G-Suite\bareCodes_files\CB.txt”;
使用(StreamWriter sw=新StreamWriter(barecords_Path,append:false))
{
软件写入(裸代码);
}
FixedFileEngine引擎=新的FixedFileEngine();
ReadBareCodes[]rst=engine.ReadFile(bareCodes\u路径);
foreach(rst中的var det)
{
Messages.InformationMessage($”日期:{det.cbDate}\nPoids网络:{det.poidsNet.ToString(“0.00”)}\n_Dix:{det.Dix}\nLot:{det.lot}”);
}
}
当然,输出格式是:011780,00
,这不是我想要的
有人能告诉我正确的方向吗?您可以使用来修改字段的转换方式。这是一个适用于您的场景的工作程序
class Program
{
[FixedLengthRecord]
class ReadBarCodes
{
[FieldFixedLength(6)]
public string cbDate;
[FieldFixedLength(6)]
[FieldConverter(typeof(MyDoubleConverter))]
public double poidsNet;
[FieldFixedLength(4)]
public string dix;
[FieldFixedLength(6)]
public string lot;
}
public class MyDoubleConverter : ConverterBase
{
public override object StringToField(string from)
{
// When importing, 'from' will contain the string from the file "011780"
return Convert.ToDouble(Double.Parse(from) / 100);
}
public override string FieldToString(object fieldValue)
{
// When exporting, do the opposite: remove the decimal point.
return ((double)fieldValue).ToString("#.##").Replace(".", "");
}
}
public static void Main(string[] args)
{
var fileContent = "(3102)011780(10)772890";
var engine = new FileHelperEngine<ReadBarCodes>();
var records = engine.ReadString(fileContent);
if (records[0].poidsNet == 117.80)
Console.WriteLine("The poidsNet field was correctly read as 117.80.");
else
Console.WriteLine("Failed to read poidsNet correctly.");
Console.WriteLine("OK");
Console.ReadKey();
}
}
类程序
{
[固定长度记录]
类读取条形码
{
[字段固定长度(6)]
公共字符串cbDate;
[字段固定长度(6)]
[FieldConverter(类型(MyDoubleConverter))]
公共双点网;
[字段固定长度(4)]
公共字符串dix;
[字段固定长度(6)]
公共地段;
}
公共类MyDoubleConverter:ConverterBase
{
公共覆盖对象StringToField(字符串来自)
{
//导入时,“from”将包含文件“011780”中的字符串
返回Convert.ToDouble(Double.Parse(from)/100);
}
公共重写字符串FieldToString(对象fieldValue)
{
//导出时,执行相反的操作:删除小数点。
return((double)fieldValue.ToString(“#.#.#.#.”).Replace(“.”,”);
}
}
公共静态void Main(字符串[]args)
{
var fileContent=“(3102)011780(10)772890”;
var engine=new FileHelperEngine();
var records=engine.ReadString(fileContent);
如果(记录[0]。poidsNet==117.80)
WriteLine(“poidsNet字段被正确读取为117.80”);
其他的
Console.WriteLine(“未能正确读取poidsNet”);
控制台。写入线(“OK”);
Console.ReadKey();
}
}
您可以使用来修改字段的转换方式。这是一个适用于您的场景的工作程序
class Program
{
[FixedLengthRecord]
class ReadBarCodes
{
[FieldFixedLength(6)]
public string cbDate;
[FieldFixedLength(6)]
[FieldConverter(typeof(MyDoubleConverter))]
public double poidsNet;
[FieldFixedLength(4)]
public string dix;
[FieldFixedLength(6)]
public string lot;
}
public class MyDoubleConverter : ConverterBase
{
public override object StringToField(string from)
{
// When importing, 'from' will contain the string from the file "011780"
return Convert.ToDouble(Double.Parse(from) / 100);
}
public override string FieldToString(object fieldValue)
{
// When exporting, do the opposite: remove the decimal point.
return ((double)fieldValue).ToString("#.##").Replace(".", "");
}
}
public static void Main(string[] args)
{
var fileContent = "(3102)011780(10)772890";
var engine = new FileHelperEngine<ReadBarCodes>();
var records = engine.ReadString(fileContent);
if (records[0].poidsNet == 117.80)
Console.WriteLine("The poidsNet field was correctly read as 117.80.");
else
Console.WriteLine("Failed to read poidsNet correctly.");
Console.WriteLine("OK");
Console.ReadKey();
}
}
类程序
{
[固定长度记录]
类读取条形码
{
[字段固定长度(6)]
公共字符串cbDate;
[字段固定长度(6)]
[FieldConverter(类型(MyDoubleConverter))]
公共双点网;
[字段固定长度(4)]
公共字符串dix;
[字段固定长度(6)]
公共地段;
}
公共类MyDoubleConverter:ConverterBase
{
公共覆盖对象StringToField(字符串来自)
{
//导入时,“from”将包含文件“011780”中的字符串
返回Convert.ToDouble(Double.Parse(from)/100);
}
公共重写字符串FieldToString(对象fieldValue)
{
//导出时,执行相反的操作:删除小数点。
return((double)fieldValue.ToString(“#.#.#.#.”).Replace(“.”,”);
}
}
公共静态void Main(字符串[]args)
{
var fileContent=“(3102)011780(10)772890”;
var engine=new FileHelperEngine();
var records=engine.ReadString(fileContent);
如果(记录[0]。poidsNet==117.80)
WriteLine(“poidsNet字段被正确读取为117.80”);
其他的
Console.WriteLine(“未能正确读取poidsNet”);
控制台。写入线(“OK”);
Console.ReadKey();
}
}