• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

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

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
goodk
Asked:
goodk
  • 4
  • 4
5 Solutions
 
Rajar AhmedConsultantCommented:
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
 
goodkAuthor Commented:
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
 
Rajar AhmedConsultantCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
goodkAuthor Commented:
Error      1      The name 'Server' does not exist in the current context
0
 
Rajar AhmedConsultantCommented:
string file = HttpContext.Current.Server.MapPath("~/abc.htm");

Open in new window

0
 
goodkAuthor Commented:
Oops, but the html file is showing up instead of content
0
 
Rajar AhmedConsultantCommented:
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
 
goodkAuthor Commented:
thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now