0)//上傳了新圖書&..."/>
溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

c# 上傳文件刪除舊文件 記事本提取目錄

發(fā)布時(shí)間:2020-06-14 16:55:20 來源:網(wǎng)絡(luò) 閱讀:734 作者:小仙女KOMons 欄目:編程語(yǔ)言


html: <input id="Fi_Fp" runat="server" name="Fi_Fp"  type="file" />

.cs:

using System.IO;

using System.Text.RegularExpressions;

using System.Windows.Forms;


 if (Fi_Fp.PostedFile.ContentLength > 0)//上傳了新圖書

        {

            b._upfile = uploadFile(Fi_Fp);

            if (isTxt == true)

            { b._txtlist = readtxtmm(b._upfile); }//重新提取目錄

            

        }

    public string uploadFile(HtmlInputFile File1)

    {

        Comm nadd = new Comm();

        if (File1.PostedFile.ContentLength > 0)

        {

            string filenamec = null;

            string fn = File1.PostedFile.FileName;

            filenamec = "File_" + nadd.getRandomFilename();

            filenamec += fn.Substring(fn.LastIndexOf("."));

            string ext = fn.Substring(fn.LastIndexOf(".") + 1).ToLower();

            if (ext == "txt") { isTxt = true; }

            if (SET(ext))

            {

                string Picpath = System.Configuration.ConfigurationManager.AppSettings["bookfile"].ToString().Trim();

                File1.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(Picpath) + filenamec);

                deleteoldfile(Server.MapPath("../bookfile/") + Session["upfile"]);//刪除舊文件

                return filenamec;

            }

            else

            {

                return "格式不正確!";

            }

        }

        else

        {

            return "";

        }

    }

public void deleteoldfile(string oldfile)

    {

        //判斷文件是不是存在

        if (File.Exists(@""+oldfile+""))

        {

            //如果存在則刪除

            File.Delete(@"" + oldfile + "");

        }

    }

 public bool SET(string extion)

    {

        string[] extAry =

           {"txt","doc","ppt","jpg","pdf","bmp","docx"

            };

        for (int i = 0; i < extAry.Length; i++)

        {

            if (extAry[i] == extion)

            {

                return true;

            }

        }

        return false;

    }

    //提取章節(jié)名及開始行號(hào)結(jié)束行號(hào)

    public string readtxtmm(string url)//url txt文件路徑

    {


        string path = Server.MapPath("../bookfile/") + url;

        string str = "第(?<value>[\\w]*?)章";//查找章節(jié)關(guān)鍵字  *號(hào)為變量

        string wz = ""; //章節(jié)及行號(hào)列表

        string[] stringlines = File.ReadAllLines(path, System.Text.Encoding.Default);

        Regex reg = new Regex(str);

        for (int i = 0; i < stringlines.Length; i++)

        {

            Match mat = reg.Match(stringlines[i].ToString());

            if (mat.Success)

            {

                if (i != 0)

                {

                    wz += "," + (i - 1) + "|" + i + "," + stringlines[i].ToString();//上一章結(jié)尾行號(hào),本章開始行號(hào)  章節(jié)名  

                }

                else

                { wz += "0," + stringlines[i].ToString(); }

            }

        }

        wz += "," + stringlines.Length + "|";

        return wz;

    }

//讀取目錄沒人并輸出

//讀取章節(jié)內(nèi)容

        string path = Server.MapPath("bookfile/") + url;

        string[] stringlines = File.ReadAllLines(path, System.Text.Encoding.Default);

        booktitle = stringlines[start].ToString();

        for (int i = start+1; i <= end; i++)

        {

            if (i < stringlines.Length)

            {

                if ( stringlines[i].ToString().Trim().Length>0)

                {


                    bookscontent.Text += "<p>" + stringlines[i].ToString() + "</p>";

                }

            }

        }

        //讀取上一頁(yè)下一頁(yè)

      

        string[] list = txtlist.Split('|');

        for (int i = 0; i < list.Length; i++)

        {

            if (list[i].ToString().Trim() != "")

            {

                string[] dang = list[i].Split(',');

                if (int.Parse(dang[0].ToString()) == start)

                {

                    if (i - 1 >= 0)

                    {

                        string[] arrlist = list[i - 1].Split(',');

                        uppage = "上一頁(yè):<a href=\"WorkerStudyView.aspx?BLid=" + blid + "&start=" + arrlist[0].ToString() + "&end=" + arrlist[2].ToString() + "\">" + arrlist[1].ToString().Trim() + "</a><br />";

                    }

                    if (i + 1 < list.Length-1)

                    {

                        string[] arrlist = list[i + 1].Split(',');

                        nextpage = "下一頁(yè):<a href=\"WorkerStudyView.aspx?BLid=" + blid + "&start=" + arrlist[0].ToString() + "&end=" + arrlist[2].ToString() + "\">" + arrlist[1].ToString().Trim() + "</a><br />";

                    }

                    break;

                }

            }

        }    


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI