Yogesh Vedpathak

Yogesh Vedpathak

  • 1.3k
  • 598
  • 28.4k

how to improve speed of file read write opeartion

Jun 12 2018 1:09 AM
how to improve speed of file read write opeartion  when file size is upto 260 kb . 
 
protected void Button1_Click(object sender, EventArgs e)
{
try
{
while (true)
{
//ClientScript.RegisterStartupScript(GetType(), "Alert", "alert('Please wait file is generating');", true);
Dictionary<string, string> dictionary;
StreamReader file = null;
file = db.ReadFile(out dictionary, TextBox1.Text, file);
if (file == null)
{
dvIsFileExist.Visible = true;
lblIsFileExist.Visible = true;
lblIsFileExist.Text = "Movement File not exist,Please check the file name";
break;
}
List<string> lines = new List<string>();
string line = null;
bool success = false;
while ((line = file.ReadLine()) != null)
{
lines.Add(line);
success = true;
}
var PostingDate = lines[0].Split(new[] { '|' }, StringSplitOptions.None);
if (DateTime.Now.Month == Convert.ToInt32(PostingDate[2].Substring(4, 2)) || DateTime.Now.Month == Convert.ToInt32(PostingDate[3].Substring(4, 2)))
{
dvIsFileExist.Visible = true;
lblIsCurrentMonth.Visible = true;
lblIsCurrentMonth.Text = "This is a current month,Please check";
break;
}
if (success)
{
NewGeneratedFileName = TextBox1.Text.ToString().Substring(0, 8) + DateTime.Now.ToString("ddMMyyHHMMss") + "R";
string filename = dictionary["Archivepath"] + NewGeneratedFileName + ".old";
for (int i = 0; i < lines.Count; i++)
{
var x = lines[i].Split(new[] { '|' }, StringSplitOptions.None);
x[2] = DateTime.Now.ToString("yyyyMMdd");
x[3] = DateTime.Now.ToString("yyyyMMdd");
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Seek(0, SeekOrigin.End);
string temp = TextBox1.Text.ToString();
if (temp.Substring(0, 4) == "MV_F")
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(x[0] + dictionary["Separator"] + x[1] + dictionary["Separator"] + x[2] + dictionary["Separator"] + x[3] + dictionary["Separator"] + x[4] + dictionary["Separator"] + x[5] + dictionary["Separator"] + x[6] + dictionary["Separator"] + x[7] + dictionary["Separator"] + x[8] + dictionary["Separator"] + x[9] + dictionary["Separator"] + x[10] + dictionary["Separator"] + x[11] + dictionary["Separator"] + x[12] + dictionary["Separator"] + x[13] + dictionary["Separator"] + x[14] + dictionary["Separator"] + x[15] + dictionary["Separator"] + x[16] + dictionary["Separator"]);
}
}
else
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(x[0] + dictionary["Separator"] + x[1] + dictionary["Separator"] + x[2] + dictionary["Separator"] + x[3] + dictionary["Separator"] + x[4] + dictionary["Separator"] + x[5] + dictionary["Separator"] + x[6] + dictionary["Separator"] + x[7] + dictionary["Separator"] + x[8] + dictionary["Separator"] + x[9] + dictionary["Separator"] + x[10] + dictionary["Separator"] + x[11] + dictionary["Separator"] + x[12] + dictionary["Separator"] + x[13] + dictionary["Separator"] + x[14] + dictionary["Separator"] + x[15] + dictionary["Separator"]);
}
}
}
UserName = Session["UserName"].ToString();
FileName = TextBox1.Text.ToString();
Reason = DropDownList1.SelectedValue.ToString();
cmd = new SqlCommand("insert into FileGenerator(FileName,Reason,GeneratedMovementFileName,GeneratedBy,GeneratedDate) output INSERTED.MovementFileID Values('" + FileName + "','" + Reason + "','" + NewGeneratedFileName + "','" + UserName + "',@GeneratedDate)", con);
cmd.Parameters.Add("@GeneratedDate", SqlDbType.DateTime).Value = DateTime.Now;
con.Open();
GRFileID = (int)cmd.ExecuteScalar();
if (GRFileID > 0)
{
IsLogged = db.LogFile(GRFileID, FileName, NewGeneratedFileName, UserName);
}
if (IsLogged)
{
IsSuccess = db.EmailSending(dictionary, NewGeneratedFileName, Reason);
}
if (IsSuccess != 0)
{
ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('File generated successfully');", true);
ClearControl();
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('There is some problem to generate file,Please contact to administrator');", true);
//ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('There is some problem to generate file,Please contact to administrator');", true);
}
con.Close();
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('There is some problem to generate file,Please contact to administrator');", true);
}
break;
}
}
catch (Exception ex)
{
string msg = ex.Message.ToString();
ClientScript.RegisterStartupScript(Page.GetType(), "", "alert('There is some problem to generate file,Please contact to administrator');", true);
}
}
void ClearControl()
{
TextBox1.Text = "";
DropDownList1.SelectedValue = "0";
}

Answers (1)