Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Aug 6, 2021, 5:46 AM EDT
Rijwan AnsariRank #82 · 24.2K reputationPosted Aug 14, 2021, 3:25 AM EDT
Satya KarkiRank #78 · 24.6K reputationPosted Aug 14, 2021, 3:27 AM EDT
Jignesh KumarRank #91 · 22.9K reputationPosted Aug 6, 2021, 8:17 AM EDT
Meena Bisht139 reputationPosted Aug 13, 2021, 9:39 AM EDT
Hello ,
You can try the below lines of code to read the txt file and convert it to a datatable.
var table = new DataTable();
var fileContents = File.ReadAllLines("yourFile");
var splitFileContents = (from f in fileContents select f.Split(':')).ToArray();
int maxLength = (from s in splitFileContents select s.Count()).Max();
for (int i = 0; i < maxLength; i++)
{
table.Columns.Add();
}
foreach (var line in splitFileContents)
{
DataRow row = table.NewRow();
row.ItemArray = (object[])line;
table.Rows.Add(row);
}
Thanks,
Ganesan CRank #369 · 6.8K reputationPosted Aug 14, 2021, 2:27 AM EDT