Solved

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

Posted on 2013-01-16
8
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:goodk
ID: 38783488
Error      1      The name 'Server' does not exist in the current context
0
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

695 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