📄 edit.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace FTP
{
public class edit : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Header;
protected System.Web.UI.WebControls.Button CancelBtn;
protected System.Web.UI.WebControls.Button SaveBtn;
protected System.Web.UI.WebControls.HyperLink homeLink;
protected System.Web.UI.WebControls.TextBox SaveAsTxt;
protected System.Web.UI.WebControls.Button SaveAs;
protected System.Web.UI.WebControls.TextBox CodeText;
private void Page_Load(object sender, System.EventArgs e)
{
// page requires a querystring like: File=somePath
if(!Page.IsPostBack)
{
// check for the File querystring parameter
if(Request.QueryString["File"] != null)
{
try
{
// read the text from the file
FileStream fs=new FileStream(Server.MapPath(Server.UrlDecode(Request.QueryString["File"].ToString())),FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs,System.Text.Encoding.Default);
// fill the CodeText text field with the file text
CodeText.Text = sr.ReadToEnd().ToString();
sr.Close();
// set the header appropriately
Header.Text = "Editing file: <b>" + Request.QueryString["File"].ToString() + "</b>";
}
catch(Exception ex)
{
FTP.ReportError("Could not open file for editing", ex.ToString(), "If the file has just been created, wait a few seconds for Windows to release the file lock");
}
}
else
Response.Write("No file parameter passed to page");
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SaveBtn.Click += new System.EventHandler(this.SaveBtn_Click);
this.CancelBtn.Click += new System.EventHandler(this.CancelBtn_Click);
this.SaveAs.Click += new System.EventHandler(this.SaveAs_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void SaveBtn_Click(object sender, System.EventArgs e)
{
try
{
FileInfo file = new FileInfo(Server.MapPath(Server.UrlDecode(Request.QueryString["File"].ToString())));
StreamWriter sw = file.CreateText();
// write the contents of the text field to the file
sw.Write(CodeText.Text);
sw.Close();
// notify user of success
Response.Write("<script>alert('File saved successfully\\nName: " + Request.QueryString["File"].ToString() + "\\nTime: " + DateTime.Now.ToString() + "'); document.location=\"edit.aspx?File=" + Request.QueryString["File"].ToString() + "\";</script>");
}
catch(Exception ex)
{
FTP.ReportError("Could not save file.", ex.ToString(), "Press back and try again");
}
}
private void CancelBtn_Click(object sender, System.EventArgs e)
{
// back to default.aspx, no save performed
Response.Redirect("default.aspx");
}
private void SaveAs_Click(object sender, System.EventArgs e)
{
if(this.SaveAsTxt.Text == "") // user gave no filename to save as
Response.Write("<script>alert('No filename to Save As!'); </script>");
else
{
try
{
FileInfo file = new FileInfo(Server.MapPath(Session["Path"].ToString() + this.SaveAsTxt.Text));
StreamWriter sw = file.CreateText();
// write the contents of the text field to the file
sw.Write(CodeText.Text);
sw.Close();
// notify user of success
Response.Write("<script>alert('File saved successfully\\nName: " + SaveAsTxt.Text + "\\nTime: " + DateTime.Now.ToString() + "'); document.location=\"edit.aspx?File=" + Server.UrlEncode(Session["Path"].ToString() + SaveAsTxt.Text) + "\";</script>");
}
catch(Exception ex)
{
FTP.ReportError("Could not save file.", ex.ToString(), "It is sufficient to enter a file name (not the complete path) when using Save As, the file is saved in the current directory.");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -