c#上传文件删除旧文件记事本提取目录-创新互联

html:

成都网站建设、做网站的关注点不是能为您做些什么网站,而是怎么做网站,有没有做好网站,给创新互联一个展示的机会来证明自己,这并不会花费您太多时间,或许会给您带来新的灵感和惊喜。面向用户友好,注重用户体验,一切以用户为中心。

.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;

  }

  //提取章节名及开始行号结束行号

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

  {

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

    string str = "第(?[\\w]*?)章";//查找章节关键字  *号为变量

    string wz = ""; //章节及行号列表

    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();//上一章结尾行号,本章开始行号  章节名

        }

        else

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

      }

    }

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

    return wz;

  }

//读取目录没人并输出

//读取章节内容

    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 += "

" + stringlines[i].ToString() + "

";

        }

      }

    }

     //读取上一页下一页

    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 = "上一页:" + arrlist[1].ToString().Trim() + "
";

          }

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

          {

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

            nextpage = "下一页:" + arrlist[1].ToString().Trim() + "
";

          }

          break;

        }

      }

    }

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


名称栏目:c#上传文件删除旧文件记事本提取目录-创新互联
网站地址:http://myzitong.com/article/djsoig.html