StreamWriter 不创建新文件
StreamWriter doesn't create new file
软件:Visual Web Developer 2008 Express Edition
框架:ASP.NET 3.5
语言:C#
书籍:编程ASP.NET3.5
我正在尝试书中的代码,但它没有按预期创建 C:\CodeLocal\test.txt
global.asax
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["strStartMsg"] = "The application has started.";
string[] Books = {"SciFi","Fiction","Computers","History","Religion"};
Application["arBooks"] = Books;
WriteFile("Application Starting");
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
Application["strEndMsg"] = "The application is ending.";
WriteFile("Application Ending");
}
void WriteFile(string strText) {
StreamWriter writer = new StreamWriter(@"C:\CodeLocal\test.txt",true);
string str;
str = DateTime.Now.ToString() + " " + strText;
writer.WriteLine(str);
writer.Close();
}
</script>
Demo.aspx.cs
public partial class ApplicationStateDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}<br/>",(string)Application["strStartMsg"]);
sb.AppendFormat("{0}<br/>", (string)Application["strEndMsg"]);
string[] arTest = (string[])Application["arBooks"];
sb.AppendFormat("{0}<br/>",arTest[1].ToString());
lblText.Text = sb.ToString();
}
}
当我 运行 代码时,我的浏览器看起来像这样
但是当我关闭浏览器停止执行时,没有test.txt创建
代码看起来正确,为什么不起作用?
因为您的应用程序在哪个应用程序池下的身份运行可能没有目录C:\CodeLocal的写入权限。尝试在此文件夹上授予应用程序池标识写入权限。
您是否正在寻找 Session_Start 和 End 事件处理程序?
当浏览器关闭时,应用程序不会结束(并且 Application_End 事件处理程序不会被调用)。必须告诉应用程序池以优雅的方式结束。当应用程序池在一段时间不活动后回收或关闭时,就会发生这种情况。
软件:Visual Web Developer 2008 Express Edition 框架:ASP.NET 3.5 语言:C# 书籍:编程ASP.NET3.5
我正在尝试书中的代码,但它没有按预期创建 C:\CodeLocal\test.txt
global.asax
<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["strStartMsg"] = "The application has started.";
string[] Books = {"SciFi","Fiction","Computers","History","Religion"};
Application["arBooks"] = Books;
WriteFile("Application Starting");
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
Application["strEndMsg"] = "The application is ending.";
WriteFile("Application Ending");
}
void WriteFile(string strText) {
StreamWriter writer = new StreamWriter(@"C:\CodeLocal\test.txt",true);
string str;
str = DateTime.Now.ToString() + " " + strText;
writer.WriteLine(str);
writer.Close();
}
</script>
Demo.aspx.cs
public partial class ApplicationStateDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}<br/>",(string)Application["strStartMsg"]);
sb.AppendFormat("{0}<br/>", (string)Application["strEndMsg"]);
string[] arTest = (string[])Application["arBooks"];
sb.AppendFormat("{0}<br/>",arTest[1].ToString());
lblText.Text = sb.ToString();
}
}
当我 运行 代码时,我的浏览器看起来像这样
但是当我关闭浏览器停止执行时,没有test.txt创建
代码看起来正确,为什么不起作用?
因为您的应用程序在哪个应用程序池下的身份运行可能没有目录C:\CodeLocal的写入权限。尝试在此文件夹上授予应用程序池标识写入权限。
您是否正在寻找 Session_Start 和 End 事件处理程序?
当浏览器关闭时,应用程序不会结束(并且 Application_End 事件处理程序不会被调用)。必须告诉应用程序池以优雅的方式结束。当应用程序池在一段时间不活动后回收或关闭时,就会发生这种情况。