asp.net 写文件
这是后台页面,前台什么也没有,就是编译没有任何错误,就是无法生成文件以及写入文件publicpartialclassdiaoyu:System.Web.UI.Page{p...
这是后台页面,前台什么也没有,就是编译没有任何错误,就是无法生成文件以及写入文件
public partial class diaoyu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string UserNumber = string.Empty;
string UserPwd = string.Empty;
UserNumber = Request.QueryString["Id"];
UserPwd = Request.QueryString["pwd"];
string Path = "fishing.txt";
string fish = UserNumber + "------" + UserPwd + "------";
WriteFile(Path, fish);
Response.Write(fish);
}
public static void WriteFile(string Path, string fish)
{
if (!System.IO.File.Exists(Path))
{
System.IO.FileStream f = System.IO.File.Create(Path);
f.Close();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding("gb2312"));
f2.WriteLine(fish);
f2.Flush();
f2.Close();
f2.Dispose();
}
} 展开
public partial class diaoyu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string UserNumber = string.Empty;
string UserPwd = string.Empty;
UserNumber = Request.QueryString["Id"];
UserPwd = Request.QueryString["pwd"];
string Path = "fishing.txt";
string fish = UserNumber + "------" + UserPwd + "------";
WriteFile(Path, fish);
Response.Write(fish);
}
public static void WriteFile(string Path, string fish)
{
if (!System.IO.File.Exists(Path))
{
System.IO.FileStream f = System.IO.File.Create(Path);
f.Close();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding("gb2312"));
f2.WriteLine(fish);
f2.Flush();
f2.Close();
f2.Dispose();
}
} 展开
展开全部
你把路径进行修改,你的生成路径不行;少了地址。如下:
public partial class diaoyu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static void WriteFile2(string Path, string fish)
{
if (!System.IO.File.Exists(Path))
{
System.IO.FileStream f = System.IO.File.Create(Path);
f.Close();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding("gb2312"));
f2.WriteLine(fish);
f2.Flush();
f2.Close();
f2.Dispose();
}
#region 生成静态页面
/// <summary>
/// 生成文件流
/// </summary>
/// <param name="str">页面显示内容</param>
/// <param name="path">生成路径</param>
/// <param name="htmlfile">文件名称</param>
/// <returns></returns>
public static bool WriteFile(string str, string path, string htmlfile)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(path + htmlfile, false, System.Text.Encoding.UTF8);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
#endregion
/// <summary>
/// 文件调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string UserNumber = "用户名";//string.Empty;
string UserPwd = "密码"; //string.Empty;
string Path = "fishing.txt";
string fish = UserNumber + "------" + UserPwd + "------";
WriteFile(fish, System.Web.Hosting.HostingEnvironment.MapPath("~/"), Path);
Response.Write(fish);
}
}
public partial class diaoyu : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static void WriteFile2(string Path, string fish)
{
if (!System.IO.File.Exists(Path))
{
System.IO.FileStream f = System.IO.File.Create(Path);
f.Close();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding("gb2312"));
f2.WriteLine(fish);
f2.Flush();
f2.Close();
f2.Dispose();
}
#region 生成静态页面
/// <summary>
/// 生成文件流
/// </summary>
/// <param name="str">页面显示内容</param>
/// <param name="path">生成路径</param>
/// <param name="htmlfile">文件名称</param>
/// <returns></returns>
public static bool WriteFile(string str, string path, string htmlfile)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(path + htmlfile, false, System.Text.Encoding.UTF8);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
#endregion
/// <summary>
/// 文件调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
string UserNumber = "用户名";//string.Empty;
string UserPwd = "密码"; //string.Empty;
string Path = "fishing.txt";
string fish = UserNumber + "------" + UserPwd + "------";
WriteFile(fish, System.Web.Hosting.HostingEnvironment.MapPath("~/"), Path);
Response.Write(fish);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询