Solved

how do we read .htm file on my web directory and display the contents

Posted on 2013-01-16
8
173 Views
Last Modified: 2013-01-25
string temp = File.ReadAllText(Server.MapPath("http://localhost:50647/Prod1/home.htm"));

The above is not working.  Is there a better way?

I want to display the entire htm file as

Response.Write(temp);
0
Comment
Question by:goodk
  • 4
  • 4
8 Comments
 
LVL 18

Assisted Solution

by:Rajar Ahmed
Rajar Ahmed earned 500 total points
ID: 38783140
http://www.dotnetspider.com/resources/22019-read-html-file-text-file-ASP-NET.aspx
string file = Server.MapPath("~/abc.htm");
        StreamReader sr;
        FileInfo fi = new FileInfo(file);
        string input = "<pre>";
        if (File.Exists(file))
        {
            sr = File.OpenText(file);
            input += Server.HtmlEncode(sr.ReadToEnd());
            sr.Close();
        }
        else { 
         input="notExis";
        }
        input += "</pre>";
        Label1.Text = input;
 [subtitle][/subtitle]

Open in new window

0
 

Author Comment

by:goodk
ID: 38783277
I have the following but it is not working - what else I need to add?

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web.Caching;
using System.Web.SessionState;
using System.Web.Profile;
using System.Reflection;
using System.Web.Administration;
using System.Data.Common;
using System.Data.OleDb;
using System.Globalization;
using System.Collections;
0
 
LVL 18

Assisted Solution

by:Rajar Ahmed
Rajar Ahmed earned 500 total points
ID: 38783324
Your missing System.IO;
Complete code,

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readhtmlInCodebehind.aspx.cs" Inherits="readhtmlInCodebehind" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class readhtmlInCodebehind : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string file = Server.MapPath("~/abc.htm");
        StreamReader sr;
        FileInfo fi = new FileInfo(file);
        string input = "<pre>";
        if (File.Exists(file))
        {
            sr = File.OpenText(file);
            input += Server.HtmlEncode(sr.ReadToEnd());
            sr.Close();
        }
        else { 
         input="notExis";
        }
        input += "</pre>";
        Label1.Text = input;
    }
}

Open in new window

0
 

Author Comment

by:goodk
ID: 38783488
Error      1      The name 'Server' does not exist in the current context
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 18

Assisted Solution

by:Rajar Ahmed
Rajar Ahmed earned 500 total points
ID: 38783503
string file = HttpContext.Current.Server.MapPath("~/abc.htm");

Open in new window

0
 

Accepted Solution

by:
goodk earned 0 total points
ID: 38784666
Oops, but the html file is showing up instead of content
0
 
LVL 18

Assisted Solution

by:Rajar Ahmed
Rajar Ahmed earned 500 total points
ID: 38798811
Change to this on your current code,
input += Server.HtmlDecode(sr.ReadToEnd());

Open in new window


Another Option,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readhtmlInCodebehind.aspx.cs" Inherits="readhtmlInCodebehind" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <%
        Response.WriteFile("~/abc.htm");
         %>
    </div>
    </form>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:goodk
ID: 38818009
thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now