472,143 Members | 1,187 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

ASP.NET 2.0 Label with C# and Textfile html code in Visual Web Developer Express 2005

Hello, I am having a problem that I believe is related to the way a stream reader object looks for a text file by default. What I am doing is using a StreamReader object to read the text of a text file which includes some html code to populate html formatted content as the text of an asp:label (<asp:label>). The reading of the text file itself goes just fine ;however, this only occurs when I use an absolute file path which will not work of course once I upload the website to a hosting service. The text file is in the same directory as both the .aspx file and the codebehind the page file in the Website directory in the Visual Studio 2005 directory structure located in "My Documents". Here is the error message that I receive when I attempt to create load the page without an absolute file path to the text file in the StreamReader object:

Expand|Select|Wrap|Line Numbers
  1.  Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Default4.txt'.
  2. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  3.  
  4. Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Default4.txt'.
  5.  
  6. Source Error:
  7.  
  8. Line 17:     protected void Page_Load(object sender, EventArgs e)
  9. Line 18:     {
  10. Line 19:         StreamReader cr = new StreamReader("Default4.txt");
  11. Line 20:         string contents = null;
  12. Line 21:         contents = cr.ReadToEnd();
  13.  
  14.  
  15. Source File: c:\Documents and Settings\Main\My Documents\Visual Studio 2005\WebSites\WebSite2\Default4.aspx.cs    Line: 19
  16.  
  17. Stack Trace:
  18.  
  19. [FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Default4.txt'.]
  20.    System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +1971213
  21.    System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +998
  22.    System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) +115
  23.    System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) +85
  24.    System.IO.StreamReader..ctor(String path) +112
  25.    _Default.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\Main\My Documents\Visual Studio 2005\WebSites\WebSite2\Default4.aspx.cs:19
  26.    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
  27.    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +45
  28.    System.Web.UI.Control.OnLoad(EventArgs e) +80
  29.    System.Web.UI.Control.LoadRecursive() +49
  30.    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3745
  31.  
  32.  
  33. Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832
Here is the code for the C# code behind the page file:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.IO;
  11.  
  12. /// <summary>
  13. /// Summary description for Default4
  14. /// </summary>
  15. public partial class _Default : System.Web.UI.Page
  16. {
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.         StreamReader cr = new StreamReader("Default4.txt");
  20.         string contents = null;
  21.         contents = cr.ReadToEnd();
  22.         Label1.Text = contents;
  23.         cr.Close();
  24.         //Label1.Text = "<p align=center>Hello World</p>";
  25.     }
  26.  
  27. }
  28.  
Here is the Code to the .aspx file:

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/Default4.aspx.cs" Inherits="_Default"%>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <script runat="server">
  6.  
  7. </script>
  8.  
  9. <html xmlns="http://www.w3.org/1999/xhtml" >
  10. <head runat="server">
  11. <style type="text/css">
  12.         .staticMenuItem
  13.         {
  14.             color:White;
  15.  
  16.             font-weight: bold;
  17.             text-align: center;
  18.  
  19.             /*border:solid 1px black;
  20.             padding:2px 4px;*/
  21.         }
  22.         .menuHover
  23.         {
  24.             color:#ffcc00;
  25.             background-color:#ff3300;
  26.         }
  27.         .dmenuHover
  28.         {
  29.             color:blue;
  30.             background-color:#ff9900;
  31.         }
  32.         .dynamicMenuItem
  33.         {
  34.             color:white;
  35.             font-weight: bold;
  36.             text-align: center;
  37.             /*background-color:#FF6600;*/
  38.             padding:2px 4px;       
  39.         }
  40.         .dynamicMenu
  41.         {
  42.             /*the background color for the dynamic menus both "popout" and non is covered by the
  43.             DynamicMenuStyle-BackColor="#FF6600" in the <asp:Menu> tag*/
  44.             /*background-color: #ffcc00;*/
  45.             /*background-color: green;*/ 
  46.             /*font-weight: bold;
  47.             text-align: center;*/           
  48.             /*border:Solid 1px black;
  49.             filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=5, OffY=5, Color='gray', Positive='true')" ;*/
  50.         }
  51.     </style>
  52.     <title>Untitled Page</title>
  53. </head>
  54. <body>
  55.     <form id="form1" runat="server">
  56.     <div>
  57.         &nbsp;
  58.         <asp:Menu ID="Menu1" runat="server" BackColor="#990000" DynamicMenuStyle-BackColor="#333333" DataSourceID="SiteMapDataSource1" StaticDisplayLevels="2"
  59.         StaticMenuItemStyle-CssClass="staticMenuItem"
  60.         StaticHoverStyle-CssClass="menuHover"
  61.         DynamicHoverStyle-CssClass="menuHover"
  62.         DynamicMenuItemStyle-CssClass="dynamicMenuItem"
  63.         DynamicMenuStyle-CssClass="dynamicMenu">
  64.             <StaticMenuItemStyle ItemSpacing="20px" />
  65.             <DynamicMenuItemStyle ItemSpacing="0px" />
  66.         </asp:Menu>
  67.         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>&nbsp;
  68.         <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
  69.  
  70.     </div>
  71.     </form>
  72. </body>
  73. </html>
  74.  
Here is the code located in the text file named Default4.txt:
Expand|Select|Wrap|Line Numbers
  1. <br /><p align=right>Hello There World!!</p>
And finally here is the file path that I must use in the StreamReader object in order to avoid generating an error:

Expand|Select|Wrap|Line Numbers
  1. C:\\Documents and Settings\\Main\\My Documents\\Visual Studio 2005\\WebSites\\WebSite2\\
Is what is going on unfixable? Also could someone experienced with uploading Visual Web Developer 2005 generated sites give me a few pointers on deploying them? Any help here would be greatly appreciated.
Oct 16 '07 #1
1 2743
Shashi Sadasivan
1,435 Expert 1GB
While assigning the file name, you have to provide it with the path of the file too.
If you do not, then it will map it from the default path.
What you need to do is to map it from the servers path, so that it will navigate from the application folder.
User server.Mappath to get the desired path. and then supply it to the stream reader.

cheers
Oct 16 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by clintonG | last post: by
5 posts views Thread by Patrick Olurotimi Ige | last post: by
1 post views Thread by pbishop | last post: by
2 posts views Thread by Progman | last post: by
3 posts views Thread by barry | last post: by
5 posts views Thread by cj | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.