2015-01-14 3 views
0

소프트웨어를 만들지 않습니다 : 시각적 웹 개발자 2008 Express Edition 프레임 워크 : ASP.NET 3.5 언어 : C# 도서 : 내가 책에서 코드를 시도하고 ASP.NET 3.5StreamWriter를 새로운 파일을

프로그래밍, 그것은 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

을 예상대로3210
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(); 
    } 
} 

내 브라우저가이

enter image description here

하지만 실행을 중지하기 위해 브라우저를 닫을 때, 더 TEST.TXT이 코드는 올바른 보이는

을 만들어 거기처럼 보이는 코드를 실행하면, 왜이게 효과가 없습니까?

+4

디버거를 사용하여 코드를 단계별로 실행하십시오. 그것은 쓰는가? – usr

+2

올바른 쓰기 권한이 있다는 것도 긍정적입니까? – Greg

+0

@Greg 관리자 권한으로 VB를 실행하면 test.txt가 생성되고 프로그램을 시작할 때 "Application Starting"이라고 씁니다. 그러나 프로그램을 중단하면 "Application Ending"이 작성되지 않습니다. 그 밖의 무엇? – Glowie

답변

3

Session_Start 및 End 이벤트 처리기를 찾으십니까?

브라우저가 닫힐 때 응용 프로그램이 끝나지 않고 Application_End 이벤트 핸들러가 호출되지 않습니다. 앱 풀은 정중 한 태도로 끝내라는 말을해야합니다. 이는 일정 시간 동안 사용하지 않으면 응용 프로그램 풀이 재생되거나 종료 될 때 발생합니다.

+0

파일을 조금씩 확인합니다 – Glowie

+0

이 웹 응용 프로그램을 로컬에서 실행 중이며 IIS에서 호스팅되지 않은 경우 시스템 트레이에서 프로세스를 중지 할 수 있습니다. – sudheeshix

+0

방금했는데 이제는 파일에 "Application Ending"을 썼습니다. – Glowie

2

응용 프로그램의 응용 프로그램 풀에서 실행중인 ID가 C : \ CodeLocal 디렉터리에 대한 쓰기 권한을 가지고 있지 않을 수 있습니다. 이 폴더의 응용 프로그램 풀 ID에 쓰기 권한을 부여하십시오.

+0

그는 오류 메시지를받지 못했습니다. – usr

관련 문제