62,074
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class ReadAllLines : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (null == Request.QueryString["fname"])
{
Response.Write("ReadAllText.aspx.cs:" + "QueryString没有值" + "<br />");
Response.Redirect("https://www.baidu.com/");
}
else
{
String filePath = Path.Combine(Server.MapPath("读取内容文件夹"), Request.QueryString["fname"]);
if (!File.Exists(filePath))
{
Response.Write("ReadAllText.aspx.cs:" + "没有这个文件。");
Response.Redirect("https://www.baidu.com/");
}
String[] content = File.ReadAllLines(filePath);
if (content.Length < 1)
{
Response.Write("ReadAllText.aspx.cs:" + "文件没有内容");
}
TextBox tb;
Literal ltl = new Literal();
ltl.Text = "<br />";
foreach (String line in content)
{
tb = new TextBox();
tb.Text = line;
line_panel.Controls.Add(tb);
line_panel.Controls.Add(ltl);
}
}
}
}
protected void save_btn_Click(object sender, EventArgs e)
{
if (null == Request.QueryString["fname"])
{
Response.Write("ReadAllText.aspx.cs:" + "QueryString没有值" + "<br />");
Response.Redirect("https://www.baidu.com/");
}
else
{
String filePath = Path.Combine(Server.MapPath("读取内容文件夹"), Request.QueryString["fname"]);
if (!File.Exists(filePath))
{
Response.Write("ReadAllText.aspx.cs:" + "没有这个文件。");
Response.Redirect("https://www.baidu.com/");
}
String content = "";
foreach (Control ctl in line_panel.Controls)
{
if (ctl is TextBox)
{
content += ((TextBox)ctl).Text + '\u0081';
}
}
content = content.Substring(0, content.Length - 1);
String[] lines = content.Split('\u0081');
File.WriteAllLines(filePath, lines);
}
}
}
<%@ Page Title="" Language="C#" MasterPageFile="~/Editor.master" AutoEventWireup="true" CodeFile="ReadAllLines.aspx.cs" Inherits="ReadAllLines" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
#line_panel {
display:block;
width:400px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="holder_main" Runat="Server">
<asp:Panel ID="line_panel" runat="server" clientidmode="Static"></asp:Panel>
<br />
<br />
<asp:Button ID="save_btn" runat="server" Text="保存文本" OnClick="save_btn_Click" />
</asp:Content>