0

Using Asp.net, I'm reading a path and showing all the files path in a runtime created Text box/boxes as follows: Search Result

These Files not just pdf but ppt, doc,docx, etc and the code which i used is:

 protected void SearchButton_Click(object sender, EventArgs e)
    { 
       string dir = @"D:\file path\";
       SearchResultPanel.Visible = true;

        try
        {
            //Set the current directory.
            Directory.SetCurrentDirectory(dir);

            //search all files in path
            string[] directoryEntries = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories);

            int fill = directoryEntries.Length - 1;

            //Total count of files & Decoration 
            SearchResultPanel.Controls.Add(new LiteralControl("<br />"));
            Label leb = new Label();
            SearchResultPanel.Controls.Add(new LiteralControl("The total files count: "));
            leb.Text = directoryEntries.Length.ToString();
            this.SearchResultPanel.Controls.Add(leb);
            SearchResultPanel.Controls.Add(new LiteralControl("<hr />"));


            //retriving the files in new labels
            for (int i = 0; i < fill; i++)
            {
                TextBox NewText = new TextBox();
                NewText.BorderStyle = BorderStyle.NotSet;
                NewText.TextMode = TextBoxMode.MultiLine;
                NewText.Width = 380;
                NewText.Height = 40;

                TextBox NewTextP = new TextBox();
                NewTextP.BorderStyle = BorderStyle.NotSet;
                NewTextP.TextMode = TextBoxMode.MultiLine;
                NewTextP.Width = 380;
                NewTextP.Height = 40;

                Button ButtonOpen = new Button();
                ButtonOpen.Text = "Open";


                //fill the text boxes with file entries
                String path = directoryEntries[i];
                NewText.Text = directoryEntries[i];
                NewTextP.Text = "Dummy Paragraph";
                **ButtonOpen.Click += OpenFile(path);**

                //new line
                SearchResultPanel.Controls.Add(new LiteralControl("<br />"));

                //show the text box
                this.SearchResultPanel.Controls.Add(NewText);
                SearchResultPanel.Controls.Add(new LiteralControl("&#09;"));
                this.SearchResultPanel.Controls.Add(NewTextP);
                SearchResultPanel.Controls.Add(new LiteralControl("&#09;"));
                this.SearchResultPanel.Controls.Add(ButtonOpen);
            }//end of for

        }//end of try
        catch (DirectoryNotFoundException ex)
        {
            Console.WriteLine("The specified directory does not exist. {0}", ex);
        }

I'm getting a problem @ ButtonOpen.Click += OpenFile(path); as how to open the path associate file on the click of OPEN button.

Any Suggessions?

Pranay B
  • 67
  • 1
  • 13
  • im not sure, I think not all browsers are allowing access to the local filesystem, so you may be in stuck. check this http://stackoverflow.com/questions/3152482/running-exe-from-javascript – Anton Semenov Jul 29 '14 at 08:53
  • Im using it for my local machine and will always be using chrome and dont want to add any JS for now. – Pranay B Jul 29 '14 at 09:03
  • My apologies, I again forgot that it is the Web App, so the handlers probably aren't initialized on postback. I'll remove my answer(just unaccept it), and then you'd better to remove this question and ask it anew - just with appropriate tags. – Eugene Podskal Jul 29 '14 at 10:26
  • Can I use a ButtonOpen.Click += Process.Start(@path.ToString()); – Pranay B Jul 29 '14 at 15:25

1 Answers1

1

Please See this solution. I have tested now with sample.Please Mark as answered if you got help from this.

I hardcoded the file path. You can get it from text box and assign to filePath variable.

This is Link Button Click event. You have to use System.IO and System .NET as extra libraries, which is for Path.getExtention() and WebClient Class.

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string filePath = "C:\\GIHAN\\Employee Profile - Senior Engineer Technology.docx";
        string fileExtention = Path.GetExtension(filePath);
        WebClient client = new WebClient();
        Byte[] buffer = client.DownloadData(filePath);
        Response.ContentType = ReturnExtension(fileExtention);

        Response.AddHeader("content-length", buffer.Length.ToString());
        Response.BinaryWrite(buffer);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }

    private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".htm":
            case ".html":
            case ".log":
                return "text/HTML";
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/ms-word";
            case ".tiff":
            case ".tif":
                return "image/tiff";
            case ".asf":
                return "video/x-ms-asf";
            case ".avi":
                return "video/avi";
            case ".zip":
                return "application/zip";
            case ".xls":
            case ".csv":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".mp3":
                return "audio/mpeg3";
            case ".mpg":
            case "mpeg":
                return "video/mpeg";
            case ".rtf":
                return "application/rtf";
            case ".asp":
                return "text/asp";
            case ".pdf":
                return "application/pdf";
            case ".fdf":
                return "application/vnd.fdf";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            case ".msg":
                return "application/msoutlook";
            case ".xml":
            case ".sdxl":
                return "application/xml";
            case ".xdp":
                return "application/vnd.adobe.xdp+xml";
            default:
                return "application/octet-stream";
        }
    }
}

}

  • Hi Gihan, very very very... thanks for the solution. I'll make the `Button` obj to `LinkButton`. Could you please guide me how to call the `LinkButton1_Click()` on that `LinkButton` obj as on runtime I'm creating the multiple instances of button for each path the `directoryEntries[i]` returns. – Pranay B Jul 30 '14 at 14:34