c#上传文件删除旧文件记事本提取目录
html:
创新互联是一家专注于网站设计制作、成都网站建设与策划设计,鄂伦春网站建设哪家好?创新互联做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:鄂伦春等地区。鄂伦春做网站价格咨询:18982081108
.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 = "第(?
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;
}
}
}
网站栏目:c#上传文件删除旧文件记事本提取目录
本文网址:http://azwzsj.com/article/gdjsph.html