使用List<>时如何传入文件名从文件名存储付款记录

发布于 2024-12-11 06:07:37 字数 4115 浏览 0 评论 0 原文

我编写了一个应用程序来迭代包含 x 个文件的目录,并将每个文件导入到我的程序中的对象中。我为每个“付款”文件创建一个类的实例,FileHelpers 库将文件的每一行作为新记录读取到列表中。如果文件是“info”,它们将被简单地移动到预设目录。我想将文件名附加到列表末尾,还是只是将其作为变量包含?

我需要知道文件的名称,因为 x 个文件中的每一项“付款”都被组合成一个固定宽度的文本文件,以加载到我们的旧住房管理系统中。

更多信息: 当我创建固定宽度文件时,我需要输出 List 中的每笔付款以及它们来自的文件的名称。我不确定该怎么做 在 FileHelpers/C# 世界中:(

EG(用于显示目的的标头 - 导出文件中不需要)

PAYMENTID PAYMENTAMOUNT REFERENCE DATE FILETYPE FILENAME
011102010 000000010000 20148366 26102011 PO SHGR1234.PO
011102011 000000020000 20148367 26102011 PP SHGF6585.PP
011102012 000000030000 20148368 26102011 DD SHGI9854.DD

有什么想法吗?下面是一些代码片段...

更新 - FileHelpers lib = http://www.filehelpers.com/

更新 2 - 用于循环文本文件并获取付款的代码

public List<SundryPayment> getOAPayments()
        {
            FileHelperEngine engine = new FileHelperEngine(typeof(SundryPayment));
            res = (SundryPayment[])engine.ReadFile(getFilePath());
            foreach (SundryPayment record in res)
            {
                OAPaymentsList.Add(record);
            }
            return OAPaymentsList;
        }

更新 2 - 加载文件的代码

public List<object> getFiles()
        {
            List<object> obj = new List<object>();
            foreach (string file in files)
            {
                fileExt = Path.GetExtension(file).ToUpper();
                filePath = Path.GetFullPath(file).ToUpper();
                fileName = Path.GetFileNameWithoutExtension(file).ToUpper();
                fullFileName = Path.GetFileName(file).ToUpper();
                fileFund = fileName.Substring(0, 4).ToUpper();

                if (fileExt == ".DIR" || fileExt == ".ERR" || fileExt == ".CRF" || fileExt == ".STA")
                {
                    //Create Info File
                    InfoFile infofile = new InfoFile(filePath);
                    obj.Add(infofile);
                }
                else if (fileExt == ".PO" || fileExt == ".PP" || fileExt == ".TDC" || fileExt == ".TCC" || fileExt == ".DD" || fileExt == ".CSH" || fileExt == ".CQE"
                    || fileExt == ".PZ")
                {
                    if (fileFund == "SHGS" || fileFund == "GGEN")
                    {
                        //Create OA Payment File
                        OAPaymentFile oafile = new OAPaymentFile(filePath);
                        obj.Add(oafile);
                    }
                    else if (fileFund == "SHGF")
                    {
                        InfoFile infofile = new InfoFile(filePath);
                        obj.Add(infofile);
                    }
                    else
                    {
                        //Create AH Payment File
                        AHPaymentFile ahfile = new AHPaymentFile(filePath);
                        //Console.WriteLine("Object Created: {0}", filePath);
                        obj.Add(ahfile);
                    }
                }
            }

            return obj;


        }

更新 2 -用于创建固定宽度文件的(原型)代码需要将 paymetns 来源的文件名放入此文件中。

public new void Create()
        {
            string fileToCreate = Path.Combine("\\\\san\\ict\\allpay\\test\\", "cash.txt");

            using (StreamWriter sw = new StreamWriter(fileToCreate))
            {
                foreach (Payment r in ArchousePayments)
                {
                    string archouseref = r.TenancyRef + r.SubAccount + r.CheckDigit;
                    string firstamount = r.AmountPaid.Replace(".", "");
                    string amount = firstamount.PadRight(10, 'x');
                    string transcode = "ALPY";
                    string date = r.PaymentDate.Substring(0, 2) + r.PaymentDate.Substring(3, 2) + r.PaymentDate.Substring(6, 4);
                    string siteref;
                    string comment;
                    sw.WriteLine(archouseref + amount + transcode + date + amount.Length);
                }

            }
        }

I've wrote an app to iterate through a directory which contains x amount of files, and import each of them into objects within my program. I create an instance of a class for each of the files which are "payment", and the FileHelpers library reads each line of the file as a new record - into a List. If the files are "info", they are simply moved to preset directories. I am wanting to either append the name of the file to the end of the List or just include this as a variable?

I need to know the name of the file, as each of the "payments" within x amount of files are combined into one fixed width text file to load into our legacy housing management system.

MORE INFO:
When I create the fixed width file, I need to output each of the payments within the List<Payment> PLUS the name of the file they came from. I am unsure of how to do this
within FileHelpers/C# world :(

E.G. (headers for display purposes - not required in export file)

PAYMENTID PAYMENTAMOUNT REFERENCE DATE FILETYPE FILENAME
011102010 000000010000 20148366 26102011 PO SHGR1234.PO
011102011 000000020000 20148367 26102011 PP SHGF6585.PP
011102012 000000030000 20148368 26102011 DD SHGI9854.DD

Any ideas? Below are some code snippets...

UPDATE - FileHelpers lib = http://www.filehelpers.com/

UPDATE 2 - Code used to loop through text file and get payments

public List<SundryPayment> getOAPayments()
        {
            FileHelperEngine engine = new FileHelperEngine(typeof(SundryPayment));
            res = (SundryPayment[])engine.ReadFile(getFilePath());
            foreach (SundryPayment record in res)
            {
                OAPaymentsList.Add(record);
            }
            return OAPaymentsList;
        }

Update 2 - Code to load files

public List<object> getFiles()
        {
            List<object> obj = new List<object>();
            foreach (string file in files)
            {
                fileExt = Path.GetExtension(file).ToUpper();
                filePath = Path.GetFullPath(file).ToUpper();
                fileName = Path.GetFileNameWithoutExtension(file).ToUpper();
                fullFileName = Path.GetFileName(file).ToUpper();
                fileFund = fileName.Substring(0, 4).ToUpper();

                if (fileExt == ".DIR" || fileExt == ".ERR" || fileExt == ".CRF" || fileExt == ".STA")
                {
                    //Create Info File
                    InfoFile infofile = new InfoFile(filePath);
                    obj.Add(infofile);
                }
                else if (fileExt == ".PO" || fileExt == ".PP" || fileExt == ".TDC" || fileExt == ".TCC" || fileExt == ".DD" || fileExt == ".CSH" || fileExt == ".CQE"
                    || fileExt == ".PZ")
                {
                    if (fileFund == "SHGS" || fileFund == "GGEN")
                    {
                        //Create OA Payment File
                        OAPaymentFile oafile = new OAPaymentFile(filePath);
                        obj.Add(oafile);
                    }
                    else if (fileFund == "SHGF")
                    {
                        InfoFile infofile = new InfoFile(filePath);
                        obj.Add(infofile);
                    }
                    else
                    {
                        //Create AH Payment File
                        AHPaymentFile ahfile = new AHPaymentFile(filePath);
                        //Console.WriteLine("Object Created: {0}", filePath);
                        obj.Add(ahfile);
                    }
                }
            }

            return obj;


        }

Update 2 - (prototype) code used to create fixed width file. Need to put filename where paymetns came from into this file

public new void Create()
        {
            string fileToCreate = Path.Combine("\\\\san\\ict\\allpay\\test\\", "cash.txt");

            using (StreamWriter sw = new StreamWriter(fileToCreate))
            {
                foreach (Payment r in ArchousePayments)
                {
                    string archouseref = r.TenancyRef + r.SubAccount + r.CheckDigit;
                    string firstamount = r.AmountPaid.Replace(".", "");
                    string amount = firstamount.PadRight(10, 'x');
                    string transcode = "ALPY";
                    string date = r.PaymentDate.Substring(0, 2) + r.PaymentDate.Substring(3, 2) + r.PaymentDate.Substring(6, 4);
                    string siteref;
                    string comment;
                    sw.WriteLine(archouseref + amount + transcode + date + amount.Length);
                }

            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2024-12-18 06:07:37

我通过在 Import 类的末尾添加另一个固定宽度记录来解决这个问题,然后通过 Path.GetFileName() 使用它来存储文件名。然后我可以根据需要将其传递到课堂上。

I figured this one one by adding another fixed width record to the end of my Import class, then used this to store the filename via Path.GetFileName(). I could then pass this into the class as required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文