using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections;
public partial class Anewfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnewfile_Click(object sender, EventArgs e)
{
if (this.txfilename.Text == string.Empty)
{
Response.Write("<script>alert('请输入名字')</script>");
}
else
{
string name = this.txfilename.Text;
string path = Server.MapPath("") + "\\" + name;//获取文件夹服务器的路径
if (Directory.Exists(path))//判断服务器下是否存在该文件夹
{
Response.Write("<script>alert('文件夹已存在')</script>");
}
else
{
DirectoryInfo folder = Directory.CreateDirectory(path);//创建文件
string time = Convert.ToString(Directory.GetCreationTime(path));//获取创建时间
string foldername = this.txfilename.Text.Substring(this.txfilename.Text.LastIndexOf("\\")+1);
Response.Write("<script>alert('成功创建"+foldername+"时间为"+time+"')</script>");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text == string.Empty)
{
Response.Write("请输入名字");
}
else
{
string name = this.TextBox1.Text;
string deletepath = Server.MapPath("") + "\\" + name;
if (Directory.Exists(deletepath))
{
Directory.Delete(deletepath);
Response.Write("<script>alert('删除成功!')</script>");
}
else
{
Response.Write("没有找到该文件夹");
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string pathname = this.TextBox2.Text;
string filename = Server.MapPath("") + "//" + pathname;
this.ListBox1.Items.Clear();
if (!Directory.Exists(filename))
{
ListBox1.Visible = false;
Response.Write("<script>alert('找不到文件夹')<script>");
}
else
{
DirectoryInfo foldinfo = new DirectoryInfo(filename);
FileSystemInfo[] dirs = foldinfo.GetFileSystemInfos();//GETFILESYSTEMINFOS方法获取文件夹中的列表,将其数据保存到FILESYSTEMINFO[]数组中
if (dirs.Length < 1)//判断文件夹是否有数据
{
Response.Write("<script>alert('"+foldinfo+"文件没有数据')<script>");
return;
}
this.ListBox1.Visible = true;
this.ListBox1.DataSource = dirs;//绑定在LISTBOX上
this.ListBox1.DataBind();
}
}
protected void Button3_Click(object sender, EventArgs e)
{
string pathname = this.TextBox3.Text;
string filename = Server.MapPath("") + "\\" + pathname;
if (!File.Exists(filename))//判断文件是否存在
{
Response.Write("<script>alert('不存在这个文件')</script>");
return;
}
try
{
File.Delete(filename);//删除
Response.Write("<script>alert('删除成功')</script>");
}
catch (Exception err)
{
Response.Write(err.ToString());
}
}
protected void Button4_Click(object sender, EventArgs e)
{
string pathname = this.TextBox4.Text;
string filename = Server.MapPath("") + "\\" + pathname;
if (!File.Exists(filename))//判断文件是否存在
{
Response.Write("<script>alert('不存在这个文件')</script>");
return;
}
else
{
this.Panel1.Visible = true;
FileInfo file = new FileInfo(filename);
this.Label1.Text = file.FullName;//获取文件路径
this.Label2.Text = file.Length+"字节";//获取文件的大小
this.Label3.Text = file.Attributes.ToString();//获取文件属性
this.Label4.Text = file.CreationTime.ToShortDateString();//获取创建的时间
this.Label5.Text = file.LastAccessTime.ToShortDateString();//上次访问的时间
}
}
protected void Button5_Click(object sender, EventArgs e)
{
string pathname = this.FileUpload1.PostedFile.FileName;
FileInfo fi = new FileInfo(pathname);//创建文件操作FILEINFO对象
string filename = fi.Name;//获取文件名
string extension = fi.Extension;//获取文件扩展名
if (extension != ".txt")
{
Response.Write("<script>alert('请打开TXT文档')</script>");
return;
}
StreamReader sr = new StreamReader(pathname,System.Text.Encoding.Default);//创建数据流读取STREAMREADER对象,并设置它的读取的编码方式
this.Label6.Text = filename + "文件中的内容如下所示";
this.Label7.Text = " " + sr.ReadToEnd();//显示内容
sr.Close();
}
protected void Button6_Click(object sender, EventArgs e)
{
string path = this.FileUpload2.PostedFile.FileName.ToString();
if (path == string.Empty)
{
Response.Write("<script>alert('请打开要修改的文档')</script>");
return;
}
FileInfo info = new FileInfo(path);
string extentsion = info.Extension.ToString();//获取文件的扩展名
if (extentsion != ".txt")
{
Response.Write("<script>alert('请打开TXT文档')</script>");
return;
}
ViewState["path"] = path;
StreamReader sr = new StreamReader(path, System.Text.Encoding.Default);//创建数据流读取STREAMREADER对象,并设置它的读取的编码方式
this.TextBox5.Text = sr.ReadToEnd();//显示内容
sr.Close();
}
protected void Button7_Click(object sender, EventArgs e)
{
try
{
string path = ViewState["path"].ToString();
StreamWriter rw = new StreamWriter(path, true, System.Text.Encoding.GetEncoding("UTF-8"));
rw.WriteLine(this.TextBox5.Text);
rw.Close();
this.TextBox5.Text = string.Empty;
Response.Write("<script>alert('数据写入成功')</script>");
ViewState["path"] = null;
}
catch
{
Response.Write("<script>alert('请选择文本')</script>");
}
}
}