1

I am searching for files in a folder and displaying the names of file in a textbox but now there is a problem. I would like to open a specific file by selecting it from the textbox. How can I achieve that? This is what I tried so far.

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script type="text/javascript" src="Reports/PageLoad.js"></script>
    <title></title>
    <link href="Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
     <div>
         <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
            <div style="width: 100%; height: 100%; margin-top: 12px;">
                <asp:HiddenField ID="hdoperatorid" runat="server" />
                <asp:HiddenField ID="hdbankid" runat="server" />
                <asp:HiddenField ID="hdCountry" runat="server" />
                <table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%;"
                    id="mainContent">
                    <tr>
                        <td valign="top" style="width: 70%;" class="BorderFrame">
                            <table style="width: 100%; border: 2px solid; border-radius: 5px; border-color: #005cb9;" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td class="HeadingText" colspan="4">
                                        <asp:Label ID="Label12" runat="server"
                                            Text="File Logs"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="4">
                                        <table style="width: 100%; border: 2px solid; border-radius: 5px; border-color: #005cb9;" cellpadding="0" cellspacing="0">


                                            <tr>
                                                <td colspan="4" class="HeadingText">
                                                    <asp:Label ID="Label16" Width="100%" runat="server" Text="Enter Details"> </asp:Label>
                                                </td>
                                            </tr>
                                            <tr>
                                <td class="label">
                                    <asp:Label ID="lblCountry" runat="server" Text="Select Country" Width="200px" CssClass="LableText"></asp:Label>
                                </td>
                                <td>
                                    <asp:DropDownList id="ddlCountry" runat="server" AutoPostBack="True"  Width="200px">
                                     </asp:DropDownList>
                                </td>
                                 </tr>
                                            <tr>
                                <td class="label">
                                    <asp:Label ID="lblLogType" runat="server" Text="Select Log Type" Width="200px" CssClass="LableText"></asp:Label>
                                </td>
                                <td>
                                    <asp:DropDownList id="ddlLogType" runat="server" AutoPostBack="True"  Width="200px">
                                     </asp:DropDownList>
                                    <%--<asp:DropDownList id="ddlLogType" runat="server"  Width="200px">
                                        <asp:ListItem>ElmaCore</asp:ListItem>
                                       <asp:ListItem>ElmaCore</asp:ListItem>
                                     </asp:DropDownList>--%>
                                </td>
                                 </tr>
                                 <tr>
                                <td class="label">
                                    <asp:Label ID="lblDate" runat="server" Text="Date" Width="200px" CssClass="LableText"></asp:Label>
                                </td>
                                <td>
                                    <telerik:RadDatePicker ID="dtDate"  Width="200px" runat="server">
                                        <Calendar ID="Calendar1" runat="server" UseColumnHeadersAsSelectors="False" UseRowHeadersAsSelectors="False"
                                            ViewSelectorText="x">
                                        </Calendar>
                                        <DateInput ID="DateInput1" runat="server" DateFormat="dd/MMM/yyyy" DisplayDateFormat="dd/MMM/yyyy">
                                        </DateInput>
                                    </telerik:RadDatePicker>
                                </td>
                                 </tr>
                                <tr>
                                <td class="label">
                                    <asp:Label ID="lblReference" runat="server" Text="Reference" Width="200px" CssClass="LableText"></asp:Label>
                                </td>
                                <td class="txtbox">
                                   <asp:TextBox ID="txtReference" ReadOnly="false" Width="200px" runat="server" CssClass="textboxbook"></asp:TextBox>

                                </td>
                                 </tr>


                                        </table>
                                    </td>

                                </tr>
                                <tr>
                                    <td colspan="4">
                                        <asp:Label ID="lblMessageLine" Width="100%" Height="20px" runat="server" CssClass="BottomDisplayMsg" />
                                    </td>
                                </tr>

                               <tr>
                                    <td colspan="4" class="HeadingText">
                                        <asp:Label ID="Label7" runat="server" Text="File Contents"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="4">
                                        <asp:TextBox id="txtContents" runat="server"  Width="1200px" CssClass="textboxbook" TextMode="MultiLine" Height="300" />
                                        <%--<telerik:RadGrid ID="grdContents" runat="server" Height="180px" AutoGenerateColumns="true"
                                            GridLines="None">
                                            <ClientSettings EnablePostBackOnRowClick="true">
                                                <Selecting AllowRowSelect="true" />
                                                <Scrolling AllowScroll="True" UseStaticHeaders="true" />
                                            </ClientSettings>
                                            <%--<MasterTableView DataKeyNames="Country,BankID,FirstName,MiddleName,LastName,Mobile,Email,Address,City,Account,Title,IDNO">
                                                <Columns>
                                                    <telerik:GridBoundColumn DataField="Country" HeaderText="Country" UniqueName="Country" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="BankID" HeaderText="BankID" UniqueName="BankID" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName" UniqueName="FirstName" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="MiddleName" HeaderText="MiddleName" UniqueName="MiddleName" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName" UniqueName="LastName" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="Mobile" HeaderText="Mobile" UniqueName="Mobile" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="Email" HeaderText="Email" UniqueName="Email" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="Address" HeaderText="Address" UniqueName="Address" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="City" HeaderText="City" UniqueName="City" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="Account" HeaderText="Account" UniqueName="Account" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="Title" HeaderText="Title" UniqueName="Title" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="IDNO" HeaderText="IDNO" UniqueName="IDNO" DefaultInsertValue="">
                                                    </telerik:GridBoundColumn>
                                                </Columns>
                                            </MasterTableView>-%>
                                        </telerik:RadGrid>--%>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td style="width: 10%;" align="center" valign="bottom" class="BorderFrame">
                            <table style="width: 100%;">
                                <tr>
                                   <td style="text-align: right">
                                                <asp:Button ID="cmdAdd" Width="100px"  runat="server" Text="Submit" CssClass="button"
                                                    OnClick="cmdAdd_Click" />
                                            </td>
                                    </tr>
                                <%--<tr>
                                   <td style="text-align: right">
                                                <asp:Button ID="cmdSend" Width="100px"  runat="server" Text="Send" CssClass="button"
                                                    OnClick="cmdSend_Click" />
                                            </td>
                                    </tr>--%>
                                     <tr>
                                            <td style="text-align: right">
                                                <asp:Button ID="btnClear" Width="100px"   runat="server" Text="Clear" CssClass="button"
                                                    OnClick="btnClear_Click" />
                                            </td>
                                       </tr>
                                        <tr>


                                            <td style="text-align: right">
                                                <asp:Button ID="btnCancel" Width="100px"  runat="server" Text="Cancel" CssClass="button"
                                                     OnClick="btnCancel_Click" />
                                            </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
    </div>
    </form>
</body>
</html>

c# code:-

 private void ProcessFile(string filename, string filePath)
    {
        try
        {
            DataRow dr;

            string line;

            string content = "";

            dr = tblFileContent.NewRow();

            // Read the file and display it line by line.
            //using (StreamReader file = new StreamReader(@"C:\Upload.txt"))
            using (StreamReader file = new StreamReader(filePath))
            {
                /*while ((line = file.ReadLine()) != null)
                {
                    content = line + "\n";

                }*/

                txtContents.Text += filename+"\n";


                file.Close();
            }



        }
}
John Cena
  • 11
  • 1

1 Answers1

1

From your current snippets you can open file like this:-

string strfullPath = System.IO.Path.Combine(filepath, filename);
using (StreamReader steamReader = new StreamReader(strfullPath))
{
    string content = steamReader.ReadToEnd();
}

Update1

If you want to open it with Notepad you need System.Diagnostics.Process.Start()

Like:

Process.Start("notepad.exe", strfullPath);

If you want to open with its own editor than:

Process.Start(strfullPath);

Some how like this:-

//Process.Start(@"c:\myfile.txt");

Update2

If files are located at remote server then You would need to be authenticated on the share folder before you can access the files. For remote server this Link is worthy.

Community
  • 1
  • 1
Afnan Ahmad
  • 2,492
  • 4
  • 24
  • 44
  • :- How do I open the file in a notepad by selecting a file from the textbox? – John Cena Dec 22 '16 at 12:56
  • :- Thank you for your reply. So, here is the thing- I am passing certain search parameters to a service. The service returns the list of files and I display the names of the files in the textarea. Now, what I want to achieve is, once the files are listed, someone can click on a file from the textbox and it opens up the file in a notepad. Also, I am accessing the files from a remote server. What would be my best option? – John Cena Dec 22 '16 at 13:05
  • @John Cena where the files are located ? At your machine or at remote server? – Afnan Ahmad Dec 22 '16 at 13:08
  • :- In a remote server. – John Cena Dec 22 '16 at 13:09
  • :- Yes, the service that returns me the files takes care of all the permissions and rights. All I need is a way to display the file in a notepad by selecting a file from the textbox. – John Cena Dec 22 '16 at 13:16
  • @John Cena are you copying the file from remote server ? – Afnan Ahmad Dec 22 '16 at 13:18
  • @John Cena If you have access to the file than `Process.Start` is all about your question. – Afnan Ahmad Dec 22 '16 at 13:19