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:
- Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Default4.txt'.
-
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.
-
-
Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Default4.txt'.
-
-
Source Error:
-
-
Line 17: protected void Page_Load(object sender, EventArgs e)
-
Line 18: {
-
Line 19: StreamReader cr = new StreamReader("Default4.txt");
-
Line 20: string contents = null;
-
Line 21: contents = cr.ReadToEnd();
-
-
-
Source File: c:\Documents and Settings\Main\My Documents\Visual Studio 2005\WebSites\WebSite2\Default4.aspx.cs Line: 19
-
-
Stack Trace:
-
-
[FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Default4.txt'.]
-
System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +1971213
-
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
-
System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) +115
-
System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) +85
-
System.IO.StreamReader..ctor(String path) +112
-
_Default.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\Main\My Documents\Visual Studio 2005\WebSites\WebSite2\Default4.aspx.cs:19
-
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
-
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +45
-
System.Web.UI.Control.OnLoad(EventArgs e) +80
-
System.Web.UI.Control.LoadRecursive() +49
-
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3745
-
-
-
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:
- using System;
-
using System.Data;
-
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.IO;
-
-
/// <summary>
-
/// Summary description for Default4
-
/// </summary>
-
public partial class _Default : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
StreamReader cr = new StreamReader("Default4.txt");
-
string contents = null;
-
contents = cr.ReadToEnd();
-
Label1.Text = contents;
-
cr.Close();
-
//Label1.Text = "<p align=center>Hello World</p>";
-
}
-
-
}
-
Here is the Code to the .aspx file:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/Default4.aspx.cs" Inherits="_Default"%>
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-
<script runat="server">
-
-
</script>
-
-
<html xmlns="http://www.w3.org/1999/xhtml" >
-
<head runat="server">
-
<style type="text/css">
-
.staticMenuItem
-
{
-
color:White;
-
-
font-weight: bold;
-
text-align: center;
-
-
/*border:solid 1px black;
-
padding:2px 4px;*/
-
}
-
.menuHover
-
{
-
color:#ffcc00;
-
background-color:#ff3300;
-
}
-
.dmenuHover
-
{
-
color:blue;
-
background-color:#ff9900;
-
}
-
.dynamicMenuItem
-
{
-
color:white;
-
font-weight: bold;
-
text-align: center;
-
/*background-color:#FF6600;*/
-
padding:2px 4px;
-
}
-
.dynamicMenu
-
{
-
/*the background color for the dynamic menus both "popout" and non is covered by the
-
DynamicMenuStyle-BackColor="#FF6600" in the <asp:Menu> tag*/
-
/*background-color: #ffcc00;*/
-
/*background-color: green;*/
-
/*font-weight: bold;
-
text-align: center;*/
-
/*border:Solid 1px black;
-
filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=5, OffY=5, Color='gray', Positive='true')" ;*/
-
}
-
</style>
-
<title>Untitled Page</title>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div>
-
-
<asp:Menu ID="Menu1" runat="server" BackColor="#990000" DynamicMenuStyle-BackColor="#333333" DataSourceID="SiteMapDataSource1" StaticDisplayLevels="2"
-
StaticMenuItemStyle-CssClass="staticMenuItem"
-
StaticHoverStyle-CssClass="menuHover"
-
DynamicHoverStyle-CssClass="menuHover"
-
DynamicMenuItemStyle-CssClass="dynamicMenuItem"
-
DynamicMenuStyle-CssClass="dynamicMenu">
-
<StaticMenuItemStyle ItemSpacing="20px" />
-
<DynamicMenuItemStyle ItemSpacing="0px" />
-
</asp:Menu>
-
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
-
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
-
-
</div>
-
</form>
-
</body>
-
</html>
-
Here is the code located in the text file named Default4.txt:
- <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:
- 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.