C# 根据文件的邮政编码值创建目录并使用 System.IO、System.Data 和传输文件CSV阅读器

发布于 2024-10-19 07:48:20 字数 3954 浏览 1 评论 0原文

现有应用程序:我有一个 C# 应用程序,它运行搜索并获取匹配的引导文件并将它们放入特定的目录中。

添加功能:让系统检查文件的邮政编码是否存在子目录,如果不存在则创建它,然后我希望将文件传输到相应的目录中。

问题:现在,没有任何文件正在传输,也没有任何子目录正在创建。什么也没发生。是语法问题还是我调用错误???

请帮忙!

这是我添加的内容:

            string targetzipdir = m_sc.get_TargetPath() + "\\" + ZIP;
            // If the zip code subdirectory doesn't exist, create it.

            if (!Directory.Exists(targetzipdir))
            {
                 Directory.CreateDirectory(targetzipdir);
            } 

         private void TransferFile(string sourceDir, string filename, string ZIP)     
         { 
            string targetFileAndPath = m_sc.get_TargetPath() + "\\" + ZIP + "\\" + fullFileName;

这是 SearchProcess.cs 文件中的更多代码,以了解更大的情况(保留了一些内容):

    public void Run()
    {
        m_sc = (SearchCriteria)m_form.Invoke(m_form.m_DelegateGetSearchCriteria);
        // Display parameters
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Corp: " + m_sc.get_Corp() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on OrderNumber: " + m_sc.get_OrderNumber() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Campaign: " + m_sc.get_Campaign() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on City: " + m_sc.get_City() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on State: " + m_sc.get_State() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Zip: " + m_sc.get_Zip() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Source Path: " + m_sc.get_SourcePath() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Target Path: " + m_sc.get_TargetPath() });

        DirSearch(m_sc.get_SourcePath());

        // Make asynchronous call to main form
        // to inform it that thread finished
        m_form.Invoke(m_form.m_DelegateThreadFinished, null);
    }

            for (int colNum = 0; colNum < expectedTypes.Count; colNum++)
            {
            if (m_sc.get_SearchOR().Equals(true))
                // Check for the Zip match
                if (m_sc.get_Zip() != "" && ZIP.Contains(m_sc.get_Zip()) == true)
                {
                    found = true;

                    string targetzipdir = m_sc.get_TargetPath() + "\\" + ZIP;
                    // If the zip code subdirectory doesn't exist, create it.

                    if (!Directory.Exists(targetzipdir))
                    {
                        Directory.CreateDirectory(targetzipdir);
                    } 

                }     // ending if (m_sc.get_SearchOR().Equals(true))
            }         //ending for loop  

    private void TransferFile(string sourceDir, string filename, string ZIP)
    {
        string fullFileName = filename + ZIP + ".pdf";
        string fullFileNameAndPath = sourceDir + "\\" + fullFileName;

        //copy matching source file into the specified subdirectory based on zip
        string targetFileAndPath = m_sc.get_TargetPath() + "\\" + ZIP + "\\" + fullFileName;
        // Copy the file if the source file exists
        if (File.Exists(fullFileNameAndPath))
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"COPYING FROM: " + fullFileNameAndPath});
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"TO: " + targetFileAndPath});
            // Do the copy, overrite the target file if it exists
            File.Copy(fullFileNameAndPath, targetFileAndPath, true);
        }
        else
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"Source file does not exist: " + fullFileNameAndPath});
        }
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] {""});
    }

感谢您的查看!任何想法将不胜感激! :)

Existing App: I have a C# app that runs a search and fetches matching lead files and throws them into a specificied directory.

Adding functionality to: have the system check to see if a subdirectory exists for the file's zip code and if it doesn't exist create it, then i want the file transfered into that respective directory.

Problem: Right now, none of the files are transferring and none of the subdirectories are being created. Nothing happens. Is it a syntax issue or am I calling it wrong???

Please help!

Here's what I added:

            string targetzipdir = m_sc.get_TargetPath() + "\\" + ZIP;
            // If the zip code subdirectory doesn't exist, create it.

            if (!Directory.Exists(targetzipdir))
            {
                 Directory.CreateDirectory(targetzipdir);
            } 

         private void TransferFile(string sourceDir, string filename, string ZIP)     
         { 
            string targetFileAndPath = m_sc.get_TargetPath() + "\\" + ZIP + "\\" + fullFileName;

Here's more of the code in the SearchProcess.cs file for the bigger picture (with some stuff left off):

    public void Run()
    {
        m_sc = (SearchCriteria)m_form.Invoke(m_form.m_DelegateGetSearchCriteria);
        // Display parameters
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Corp: " + m_sc.get_Corp() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on OrderNumber: " + m_sc.get_OrderNumber() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Campaign: " + m_sc.get_Campaign() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on City: " + m_sc.get_City() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on State: " + m_sc.get_State() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Zip: " + m_sc.get_Zip() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Source Path: " + m_sc.get_SourcePath() });
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] { "Search on Target Path: " + m_sc.get_TargetPath() });

        DirSearch(m_sc.get_SourcePath());

        // Make asynchronous call to main form
        // to inform it that thread finished
        m_form.Invoke(m_form.m_DelegateThreadFinished, null);
    }

            for (int colNum = 0; colNum < expectedTypes.Count; colNum++)
            {
            if (m_sc.get_SearchOR().Equals(true))
                // Check for the Zip match
                if (m_sc.get_Zip() != "" && ZIP.Contains(m_sc.get_Zip()) == true)
                {
                    found = true;

                    string targetzipdir = m_sc.get_TargetPath() + "\\" + ZIP;
                    // If the zip code subdirectory doesn't exist, create it.

                    if (!Directory.Exists(targetzipdir))
                    {
                        Directory.CreateDirectory(targetzipdir);
                    } 

                }     // ending if (m_sc.get_SearchOR().Equals(true))
            }         //ending for loop  

    private void TransferFile(string sourceDir, string filename, string ZIP)
    {
        string fullFileName = filename + ZIP + ".pdf";
        string fullFileNameAndPath = sourceDir + "\\" + fullFileName;

        //copy matching source file into the specified subdirectory based on zip
        string targetFileAndPath = m_sc.get_TargetPath() + "\\" + ZIP + "\\" + fullFileName;
        // Copy the file if the source file exists
        if (File.Exists(fullFileNameAndPath))
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"COPYING FROM: " + fullFileNameAndPath});
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"TO: " + targetFileAndPath});
            // Do the copy, overrite the target file if it exists
            File.Copy(fullFileNameAndPath, targetFileAndPath, true);
        }
        else
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"Source file does not exist: " + fullFileNameAndPath});
        }
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] {""});
    }

Thanks for looking! Any ideas would be greatly appreciated! :)

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

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

发布评论

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

评论(1

揽清风入怀 2024-10-26 07:48:20

我发现问题了!我在错误的位置创建目录的代码。它位于邮政编码匹配的 If 语句下,仅当邮政编码匹配并且邮政编码是搜索中的可选字段时才会执行。我将所有代码移至 TransferFile 方法下,并且运行良好。此外,数据的源目录必须指向 C:\Documents and Settings\bmccarthy\Desktop\sourcedir 并且文件必须采用特殊格式,位于该目录的子目录下
C:\Documents and Settings\bmccarthy\Desktop\sourcedir\AMLDailyHotLeads20101001

这是重写的代码:

            // Copy the file if ANY of the search criteria have been met
            if (found)
            {

                m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"FOUND: Order_No: " + Order_No +
                                                                        " barcode: " + barcode +
                                                                        " MailerCode: " + MailerCode +
                                                                        " AgentID: " + AgentID +
                                                                        " City: " + City +
                                                                        " State: " + State +
                                                                        " ZIP: " + ZIP});
                //passes values to TransferFile 
                TransferFile(directory, barcode, AgentID, ZIP);
            }
        } // end for that finds each matching record 

    }

    // find and copy the file to the target directory string ZIP
    private void TransferFile(string sourceDir, string filename, string AgentID, string ZIP)
    {
        string fullFileName = filename + ".pdf";
        string fullFileNameAndPath = sourceDir + "\\" + fullFileName;
        string targetFileAndPath;

            // adds the given lead's agentid and zip code to the targetpath string 
            string targetzipdir = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP;

            // If the given lead's zip code subdirectory doesn't exist, create it.
            if (!Directory.Exists(targetzipdir))
            {
                Directory.CreateDirectory(targetzipdir);
            }

            targetFileAndPath = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP + "\\" + fullFileName;

        // Copy the file if the source file exists
        if (File.Exists(fullFileNameAndPath))
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"COPYING FROM: " + fullFileNameAndPath});
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"TO: " + targetFileAndPath});

            // Copy the file and over-write the target file if it exists
            File.Copy(fullFileNameAndPath, targetFileAndPath, true);
        }
        else
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"Source file does not exist: " + fullFileNameAndPath});
        }
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] {""});
    }

I found the problem! I had the code for creating the directory in the wrong place. It was under the If statement for Zip Code Matching and would only execute if the zip code was a match and the zip code is an optional field in the search. I moved all the code under the TransferFile Method and it worked fine. Also the data's source directory had to be pointing to C:\Documents and Settings\bmccarthy\Desktop\sourcedir AND the files had to be in a special format under a subdirectory of that directory at
C:\Documents and Settings\bmccarthy\Desktop\sourcedir\AMLDailyHotLeads20101001

Here's the rewritten code:

            // Copy the file if ANY of the search criteria have been met
            if (found)
            {

                m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"FOUND: Order_No: " + Order_No +
                                                                        " barcode: " + barcode +
                                                                        " MailerCode: " + MailerCode +
                                                                        " AgentID: " + AgentID +
                                                                        " City: " + City +
                                                                        " State: " + State +
                                                                        " ZIP: " + ZIP});
                //passes values to TransferFile 
                TransferFile(directory, barcode, AgentID, ZIP);
            }
        } // end for that finds each matching record 

    }

    // find and copy the file to the target directory string ZIP
    private void TransferFile(string sourceDir, string filename, string AgentID, string ZIP)
    {
        string fullFileName = filename + ".pdf";
        string fullFileNameAndPath = sourceDir + "\\" + fullFileName;
        string targetFileAndPath;

            // adds the given lead's agentid and zip code to the targetpath string 
            string targetzipdir = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP;

            // If the given lead's zip code subdirectory doesn't exist, create it.
            if (!Directory.Exists(targetzipdir))
            {
                Directory.CreateDirectory(targetzipdir);
            }

            targetFileAndPath = m_sc.get_TargetPath() + "\\" + AgentID + "\\" + ZIP + "\\" + fullFileName;

        // Copy the file if the source file exists
        if (File.Exists(fullFileNameAndPath))
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"COPYING FROM: " + fullFileNameAndPath});
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"TO: " + targetFileAndPath});

            // Copy the file and over-write the target file if it exists
            File.Copy(fullFileNameAndPath, targetFileAndPath, true);
        }
        else
        {
            m_form.Invoke(m_form.m_DelegateAddString, new Object[] {"Source file does not exist: " + fullFileNameAndPath});
        }
        m_form.Invoke(m_form.m_DelegateAddString, new Object[] {""});
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文