c#-写入文件并格式化行.建议

想知道您是否对我提出的建议有更好的建议.

我需要以特定格式编写文本文件
每个字段必须从指定的位置开始,并且必须用空格填充,直到下一个.

例:

        Field            Position in the row
        Name             1  
        Surname          20
        Address          50
        Country          90
        DOB              120
        MaritalStatus    160

以下是我的原型尝试,是否有更整洁的更好方法?
是否需要在单元测试中测试行中的位置是否正确?

有什么建议么?

              class Program
                {
                    static void Main(string[] args)
                    {
                        Customer customer=new Customer();
                        customer.Name = "Jo";
                        customer.Surname = "Bloggs";
                        customer.Address = " 1 NewYork Road";
                        customer.Country = "UK";
                        customer.DOB = "29/04/1990";
                        customer.MaritalStatus = "Married";

                        StringBuilder sb=new StringBuilder();
                        CreateHeader(customer,sb);
                        sb.AppendLine("");
                        CreateRow(customer, sb);
                        sb.AppendLine("");

                        IOExtensions.WriteToFile(sb.ToString(), "TestFile.txt");

                    }

                    private static void CreateHeader(Customer customer,StringBuilder sb)
                    {
                        /*
                           * 
                          Field            Position in the row
                          Name             1    
                          Surname          20
                          Address          50
                          Country          90
                          DOB              120
                          MaritalStatus    160

                           */
                        //First Field
                        sb.Append(FormatValue("Name", 19));
                        sb.Append(FormatValue("Surname", 29));
                        sb.Append(FormatValue("Address", 39));
                        sb.Append(FormatValue("Country", 29));
                        sb.Append(FormatValue("DOB", 39));

                        //Last field does not matter
                        sb.Append(FormatValue("MaritalStatus", 9));


                    }
                    private static void CreateRow(Customer customer, StringBuilder sb)
                    {
                        /*
                           * 
                          Field            Position in the row
                          Name           1  
                          Surname          20
                          Address          50
                          Country          90
                          DOB              120
                          MaritalStatus    160

                           */
                        //First Field
                        sb.Append(FormatValue(customer.Name, 19));
                        sb.Append(FormatValue(customer.Surname, 29));
                        sb.Append(FormatValue(customer.Address, 39));
                        sb.Append(FormatValue(customer.Country, 29));
                        sb.Append(FormatValue(customer.DOB, 39));

                        //Last field does not matter
                        sb.Append(FormatValue(customer.MaritalStatus, 19));


                    }

                    private static string FormatValue(string value, int maxLength)
                    {
                        //TODO ADD OTHER STUFF HERE
                        return value.PadRight(maxLength, ' ');
                    }
                }

                public static class IOExtensions
                {
                    public static void WriteToFile(string text, string path)
                    {
                        using (var fs = File.CreateText(path))
                        {
                            fs.Write(text);
                        }
                    }
                }
                public class Customer
                {
                    public string Name { get; set; }
                    public string Surname { get; set; }
                    public string Address { get; set; }
                    public string Country { get; set; }
                    public string DOB { get; set; }
                    public string MaritalStatus { get; set; }
                }
            }
最佳答案
我一直在这样做,这是我的建议….带上您的课,重写“ ToString”函数或创建一个自定义函数,称为“别的”.使用“ PadRight”字符串函数创建带有右填充的固定长度字段.

示例(您需要将其余字段添加到ToString中):

            public class Customer
            {
                public string Name { get; set; }
                public string Surname { get; set; }
                public string Address { get; set; }
                public string Country { get; set; }
                public string DOB { get; set; }
                public string MaritalStatus { get; set; }

                public override string ToString()
                {
                    return String.Format("{0}{1}{2}",
                        Name.PadRight(20),
                        Surname.PadRight(30),
                        Address.PadRight(40)
                        );
                }
            }

注意:我们使用长度来控制每个字段的开始位置.如果第一个字段为20个字符,则下一个字段将从21开始,并达到您定义的长度.

现在,遍历数据并填充对象并调用customer.ToString()以写出格式化的字符串.

转载注明原文:c#-写入文件并格式化行.建议 - 代码日志