ASP.NET檔案操作

來源:互聯網
上載者:User

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 = "&nbsp;&nbsp;" + 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>");
            }
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.