C#通过CSV文件/ API调用循环

时间:2015-09-17 00:01:59

标签: c# csv

我正在编写一个控制台应用程序,它将查看csv文件的三个不同文件夹(PathA,PathB,PathC)并通过电子邮件发送内部客户。我需要一些逻辑方面的帮助。对于PathA中的每个电子邮件地址,我想调用api发送一封TemplateA电子邮件。对于PathB中的每个电子邮件地址,它需要TemplateB等。我不确定我应该如何引用每个路径和每个模板。

    static void RunTask(string[] args)
    {

       // List<DM.SendEmailRequest> response = GetEmailRequest();
        List<PathElement> paths = PathsConfig.GetCurrentPathConfiguration();
        foreach (PathElement folder in paths)//looks at every folder within main folder
        {
            foreach (string file in Directory.EnumerateFiles(folder.Path, "*.csv"))//looks at every file with the extension ".csv" in each folder
            {
                Debug.Write("\n" + file + "\n"); //writes out file names
                using (StreamReader sr = new StreamReader(file))
                {
                    String line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] parts = line.Split(',');

                        string email = parts[1];
                        Debug.Write(email + "\n");//writes out email column
                        foreach (var customer in email)
                        {
                            if (PathA){ //how do I actually reference this path?
                                //call API and send TemplateA
                        }
                            else if (PathB)
                            {
                                //call API and send TemplateB
                            }
                            else
                            {
                                //call API and send TempateC
                            }
                    }


                    }
                }

            }
        }


    }


    private static List<DM.SendEmailRequest> GetEmailRequest()
    {
        using (var httpClient = GetHttpClient())
        {
            DM.SendEmailRequest request = new DM.SendEmailRequest();
            request.TemplateId = //A, B or C;
            request.RecipientAddress = //current recipient;


            var response = GetResponseString(httpClient, "SendEmail", new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"));

            return string.IsNullOrWhiteSpace(response) ? default(List<DM.SendEmailRequest>) : JsonConvert.DeserializeObject<List<DM.SendEmailRequest>>(response);
        }
    }

2 个答案:

答案 0 :(得分:0)

以下是我要放在Using {}

中的所有内容
using (StreamReader sr = new StreamReader(file))
{
    String line;
    List<string> myEmailList = new List<string>();

    while ((line = sr.ReadLine()) != null)
    {
        string[] parts = line.Split(',');
        string email = parts[1];
        Debug.Write(email + "\n");//writes out email column
        myEmailList.Add(email);
    }
    foreach (var customer in myEmailList)
    {
        if (folder.path.Contains("first")){ 
             //Do stuff with "Customer"
             //call API and send TemplateA
        }    
        else if(folder.path.Contains("second"))
        {
             //Do stuff with "Customer"
             //call API and send TemplateB
        }
        else
        {
             //Do stuff with "Customer"
             //call API and send TempateC
        }
    }
}

另外,你可以像下面这样做

using (StreamReader sr = new StreamReader(file))
{
    String line;

    while ((line = sr.ReadLine()) != null)
    {
        string[] parts = line.Split(',');
        string email = parts[1];
        Debug.Write(email + "\n");//writes out email column
        if (folder.path.Contains("first")){
             //Do stuff with "email" 
             //call API and send TemplateA 
        }    
        else if(folder.path.Contains("second"))
        {
             //Do stuff with "email"
             //call API and send TemplateB
        }
        else
        {
             //Do stuff with "email"
            //call API and send TempateC
        }
    }
}

选项1将读取整个文件,收集该文件夹中所有CSV的所有电子邮件地址并构建它们的列表,然后遍历该列表,在完成所有文件读取后发送电子邮件(我建议这个,因为它可以让你获得完整列表,在发送所有电子邮件之前检查重复项(如果需要)等。

选项2将在当前行中向客户发送一封电子邮件,然后再进入该文件的下一行。

答案 1 :(得分:0)

在我看来,你需要做这样的事情:

static void RunTask(string[] args)
{
    var templates = new Dictionary<string, string>()
    {
        { "C:\\test\\first", "A" },
        { "C:\\test\\second", "B" },
        { "C:\\test\\third", "C" },
    };

    List<PathElement> paths = PathsConfig.GetCurrentPathConfiguration();
    foreach (PathElement folder in paths)//looks at every folder within main folder
    {
        foreach (string file in Directory.EnumerateFiles(folder.Path, "*.csv"))//looks at every file with the extension ".csv" in each folder
        {
            Debug.Write("\n" + file + "\n"); //writes out file names
            using (StreamReader sr = new StreamReader(file))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] parts = line.Split(',');
                    string email = parts[1];
                    Debug.Write(email + "\n");//writes out email column
                    GetEmailRequest(templates[folder.Path], email);
                }
            }

        }
    }
}

private static List<DM.SendEmailRequest> GetEmailRequest(string templateId, string recipientAddress)
{
    using (var httpClient = GetHttpClient())
    {
        DM.SendEmailRequest request = new DM.SendEmailRequest();
        request.TemplateId = templateId
        request.RecipientAddress = recipientAddress

        var response = GetResponseString(httpClient, "SendEmail", new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"));

        return string.IsNullOrWhiteSpace(response) ? default(List<DM.SendEmailRequest>) : JsonConvert.DeserializeObject<List<DM.SendEmailRequest>>(response);
    }
}

我还认为您可以将RunTask方法归结为此:

static void RunTask(string[] args)
{
    var templates = new Dictionary<string, string>()
    {
        { "C:\\test\\first", "A" },
        { "C:\\test\\second", "B" },
        { "C:\\test\\third", "C" },
    };

    var responses =
    (
        from folder in PathsConfig.GetCurrentPathConfiguration()
        from file in Directory.EnumerateFiles(folder.Path, "*.csv")
        from line in File.ReadLines(file)
        let email = line.Split(',')[1]
        select GetEmailRequest(templates[folder.Path], email)
    ).ToArray();
}
相关问题