Write Rows from DataTable to Text File

前端 未结 6 1578
面向向阳花
面向向阳花 2021-02-14 11:31
public void GenerateDetailFile()
{
  if (!Directory.Exists(AppVars.IntegrationFilesLocation))
  {
    Directory.CreateDirectory(AppVars.IntegrationFilesLocation);
  }

          


        
6条回答
  •  迷失自我
    2021-02-14 12:08

    Try this:

    To write the DataTable rows to text files in the specific directory

                var dir = @"D:\New folder\log";  // folder location
    
                if (!Directory.Exists(dir))  // if it doesn't exist, create
                    Directory.CreateDirectory(dir);
    
                foreach (DataRow row in dt.Rows)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        result.Append(row[i].ToString());
                        result.Append(i == dt.Columns.Count - 1 ? "\n" : ",");
                    }
                    result.AppendLine();
                }
                string path = System.IO.Path.Combine(dir, "item.txt");
                StreamWriter objWriter = new StreamWriter(path, false);
                objWriter.WriteLine(result.ToString());
                objWriter.Close();
    

提交回复
热议问题