protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Length > 0)
{
DataTable dt; DataRow dr = null;
FileInfo fileObj = new FileInfo(TextBox1.Text.Trim());
long size = fileObj.Length / 1024;
loggedUser = "Administrator";
folderPath = System.Web.Hosting.HostingEnvironment.MapPath("~/" +
loggedUser);
System.IO.DirectoryInfo dirObj = new DirectoryInfo(folderPath);
if (!dirObj.Exists) dirObj.Create();
try
{
fileObj.CopyTo(folderPath + "\\" + fileObj.Name);
}
catch (Exception ee)
{
TextBox1.Text = "";
string error = ee.Message.ToString();
Response.Write(" "); return;
}
if (GridView1.Rows.Count == 0)
{
dt = new DataTable();
DataColumn dc1 = new DataColumn("File Name", typeof(string));
DataColumn dc2 = new DataColumn("File Size", typeof(string));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dr = dt.NewRow();
dr["File Name"] = TextBox1.Text.ToString().Trim();
if (size > 0) dr["File Size"] = size.ToString() + " KB";
else
{
dr["File Size"] = fileObj.Length.ToString() + " Bytes";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
int count = GridView1.Rows.Count;
dt = new DataTable();
DataColumn dc1 = new DataColumn("File Name", typeof(string));
DataColumn dc2 = new DataColumn("File Size", typeof(string));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
for (int i = 0; i < count; i++)
{
dr = dt.NewRow();
dr["File Name"] = GridView1.Rows[i].Cells[1].Text;
dr["File Size"] = GridView1.Rows[i].Cells[2].Text; dt.Rows.Add(dr);
}
}
dr = dt.NewRow();
dr["File Name"] = TextBox1.Text.ToString().Trim();
if(size > 0) dr["File Size"] = size.ToString() + " KB";
else
{
dr["File Size"] = fileObj.Length.ToString() + " Bytes";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
TextBox1.Text = "";
}
}