.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

File Search from folder and Data Bind in GridView and DownLoad file

Posted By:Shashi Ray       Posted Date: September 08, 2011    Points: 40    Category: File Operation    URL: http://www.dotnetspark.com  

File Search from folder and Data Bind in GridView and Download file using C#
 

File Search from folder and Data Bind in GridView and DownLoad file

protected void Button1_Click(object sender, EventArgs e)
{
    String searchName = TextBox1.Text.ToString();
    DirectoryInfo myDir = new DirectoryInfo(@"D:\shashi\Search");
    SearchDirectories(myDir, searchName);

}

File Search and Data Bind in GridView and NavigateURL in GridView

public void SearchDirectories(DirectoryInfo dir, String target)
{
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    DataRow dr = dt.NewRow();
    DataColumn dc = new DataColumn("a", typeof(string));
    dc = new DataColumn("b", typeof(string));
    dt.Columns.Add("a");
    dt.Columns.Add("b");

    FileInfo[] files = dir.GetFiles();
    int i = 0;
    foreach (FileInfo file in files)
    {
        if (file.Name.ToLower().IndexOf(target.ToLower()) > -1)
        {
            string[] f = Directory.GetFiles(@"D:\shashi\Search", file.Name, SearchOption.AllDirectories);
            dt.Rows.Add(file.Name);
            dt.Rows[i][1] = f[0];
            i++;
        }
    }

    if (dt.Rows.Count > 0)
    {
        ds.Tables.Add(dt);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        ds.Dispose();
    }
    else
    {
        GridView1.DataSource = null;
        GridView1.DataBind();
        MessageBox("File not found");
    }
}

 

DownLoad File

   

protected void LinkButton_Open_File_Click(object sender, EventArgs e)
{
GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent;
string DownLoadFileName = row.Cells[0].Text;
string ext = DownLoadFileName.Split('.').GetValue(1).ToString();

if (ext != null)
{
    Response.ClearContent();
    switch (ext.ToLower())
    {
        case "htm":
        case "html":
        case "log":
            Response.ContentType = "text/HTML";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;

        case "txt":
            Response.ClearContent();
            Response.ContentType = "text/plain";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            //Response.WriteFile(DownLoadFileName);
            break;

        case "doc":
            Response.ContentType = "application/Microsoft Office Word";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;

        case "docx":
            Response.ContentType = "application/Microsoft Office Word";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;
        case "rtf":
            Response.ContentType = "application/Microsoft Office Word";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;

        case "xlsx":
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;

        case "xls":
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);

            break;

        case "pdf":
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);

            break;

        case "jpg":
        case "jpeg":
        case "png":
        case "bmp":
        case "gif":
            Response.ContentType = "application/image";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;
        case "swf":
            Response.ContentType = "application/x-shockwave-flash";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;

        case "zip":
            Response.ContentType = "application/winzip";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;

        case "rar":
            Response.ContentType = "application/winrar";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;

        case "exe":
            Response.ContentType = "application/executable";
            Response.AddHeader("content-disposition", "attachment;filename=" + DownLoadFileName);
            Response.TransmitFile(DownLoadFileName);
            break;


        default:
            MessageBox("The file is not an allowed");
            break;

    }
}

Shashi Kant Ray



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend