代码如下:
1. Register.cs
using System;
using System.IO;namespace myComponents
{
/// <summary>
/// Register 的摘要说明。
/// </summary>
public class Register
{
public static void doRegister( string  firstName, string lastName, string favColor )
{
string strPath=@"d:\userlist.txt";
StreamWriter strmFile; strmFile = File.AppendText(strPath);
strmFile.Write( "first name: " + firstName + Environment.NewLine );
strmFile.Write( "last name:" + lastName + Environment.NewLine );
strmFile.Write( "favorite color" + favColor + Environment.NewLine);
strmFile.Write( "==============" + Environment.NewLine );
strmFile.Close();
}
}
}2.UserRegistration.aspx<%@ Page language="c#" %>
<%@ Import namespace="myComponents" %><Script runat=server>
void Button_Click(Object sender,EventArgs e)
{
Register.doRegister( txtFirstName.Text, txtLastName.Text, txtFavColor.Text);
}
</Script><html>
<head><title>UserRegistration.aspx</title></head>
<body><form runat=server>
First Name:
<br>
<asp:TextBox ID="txtFirstName" Runat=server /><p></p>
Last Name:
<br>
<asp:TextBox ID="txtLastName" Runat=server /><p></p>
Favorite Color:
<br>
<asp:TextBox ID="txtFavColor" Runat=server /><p></p>
<asp:Button Text="Register!" OnClick="Button_Click" Runat=server />
</form></body>
</html>
-----------------------------------------------------------------------------------
错误如下:“/ASP.NET揭露”应用程序中的服务器错误。
--------------------------------------------------------------------------------对路径“d:\userlist.txt”的访问被拒绝。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.UnauthorizedAccessException: 对路径“d:\userlist.txt”的访问被拒绝。 ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。 若要授予 ASP.NET 对文件的写访问权,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。源错误: 
行 14:  StreamWriter strmFile;
行 15: 
行 16:  strmFile = File.AppendText(strPath);
行 17:  strmFile.Write( "first name: " + firstName + Environment.NewLine );
行 18:  strmFile.Write( "last name:" + lastName + Environment.NewLine );
 源文件: d:\inetpub\wwwroot\asp.net揭露\ch6\register.cs    行: 16 堆栈跟踪: 
[UnauthorizedAccessException: 对路径“d:\userlist.txt”的访问被拒绝。]
   System.IO.__Error.WinIOError(Int32 errorCode, String str) +393
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) +888
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) +44
   System.IO.StreamWriter.CreateFile(String path, Boolean append) +55
   System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) +49
   System.IO.StreamWriter..ctor(String path, Boolean append) +66
   System.IO.File.AppendText(String path) +32
   myComponents.Register.doRegister(String firstName, String lastName, String favColor) in d:\inetpub\wwwroot\asp.net揭露\ch6\register.cs:16
   ASP.UserRegistration_aspx.Button_Click(Object sender, EventArgs e) +61
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573 

解决方案 »

  1.   

    上述错误是提交后产生的:)我试过做如下解决:对d:\userlist.txt授予 ASP.NET 对文件的写访问权,但是未能解决,错误依旧.
    请大家指点:)在线等
    谢谢!
      

  2.   

    分区是NTFS的有时访问时要一些用户权限,例如:IUser_... 或ASPNET等账户。加入即可。
      

  3.   

    2种方法:
    1、给你的ASPNet帐户读写NTFS分区的权限2、使用impersonate来扮演某个有NTFS分区读写权限的帐号
      

  4.   

    to possible_Y(████本人签名需要刮开,方可看到) :方法一用了,不行啊,为什么呢?