Processing comma seperated files in Asp.net

Sandhyab
Posted by Sandhyab under ASP.NET category on | Points: 40 | Views : 1024
aspx:
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>

Code Behind:
Page Load:

protected void Page_Load(object sender, EventArgs e)
{
string hdata = string.Empty;

string ddata = string.Empty;
string inputString;
string rcvpath = ConfigurationManager.AppSettings["recv"].ToString();
string archpath = ConfigurationManager.AppSettings["arch"].ToString();
string writepath = ConfigurationManager.AppSettings["write"].ToString();

DirectoryInfo dir = new DirectoryInfo(rcvpath);
if (!dir.Exists) Response.Write("No Directory Exists");
else if (dir.GetFiles().Count() <= 0) Response.Write("No file Exists in the current directory");
else
{
foreach (FileInfo flInfo in dir.GetFiles())
{
ProcessData(flInfo,rcvpath,archpath,writepath);
}
}
}

Process Data Methods:

private void ProcessData(FileInfo flInfo, string rcvpath, string archpath, string writepath)
{
string myfile = flInfo.Name;
ReadData(flInfo, rcvpath);
MoveToArchive(flInfo, rcvpath,archpath);
WriteToFile(writepath);
BindToGrid();
}


Binding to grid:
private void BindToGrid()
{
GridView1.DataSource = listhobj;
GridView1.DataBind();
GridView2.DataSource = listdObj;
GridView2.DataBind();
}


Read Data:

 private void ReadData(FileInfo flInfo, string rcvpath)
{
Response.Write(flInfo.Name);
string myfile = flInfo.Name;
string inputString;

using (StreamReader streamReader = File.OpenText(rcvpath + " \\" + myfile))
{
inputString = streamReader.ReadLine();

while ((inputString != null))
{
if (inputString.Contains("#header"))
{
HeaderData objhdata = new HeaderData();
string[] row = inputString.Split(',');
objhdata.columnA = row[1];

listhobj.Add(objhdata);

}

if (inputString.Contains("#data"))
{
Details objdata = new Details();
string[] row = inputString.Split(',');
objdata.Column1 = row[1];
objdata.Column2 = row[2];
objdata.Column3 = row[3];
listdObj.Add(objdata);
}
inputString = streamReader.ReadLine();
}
}
}

Move to Archieve:
 private void MoveToArchive(FileInfo flInfo, string rcvpath,string archpath)
{
File.Move(rcvpath + "\\" + flInfo.Name, archpath + "\\" + flInfo.Name);
}

Write Files:
 private void WriteToFile(string writepath)
{
string hdata = string.Empty;

string ddata = string.Empty;

foreach (HeaderData h in listhobj)
{
hdata += h.columnA + Environment.NewLine + " *** " + Environment.NewLine;

}
foreach (Details d in listdObj)
{
ddata += d.Column1 + d.Column2 + d.Column3 + Environment.NewLine + " **** " + Environment.NewLine;
}
File.WriteAllText(writepath + "\\HeaderFile.txt", hdata);
File.WriteAllText(writepath + "\\DetailsFile.txt", ddata);

}



appsetting in web.config:
 <appSettings>
<add key="recv" value="D:\ProcessFiles\Recieve\"/>
<add key="arch" value="D:\ProcessFiles\Archieve"/>
<add key="write" value="D:\ProcessFiles\WriteFiles"/>
</appSettings>

Comments or Responses

Login to post response