Xml文件操作和文件操作(FileInfo类)
1. 上传文件
2. 写Xml文件
3. 文件拷贝
1. //上传
protected void picUp_Click(object sender, EventArgs e)
{
BLL.tblCase bCase = new BLL.tblCase();
Model.tblCase mCase = new Model.tblCase();
if (picFile.PostedFile.ContentLength != 0)
{
//案例文件夹
string strTitle = "";
if (ddltitle.SelectedItem.Text == "请选择")
{
ClientScript.RegisterStartupScript(GetType(), "", "<script type='text/javascript'>alert('请选择案例名称');</script>");
return;
// if (txtTitle.Text.Trim() == "")
// { }
//else
//{
// int num = bCase.GetMaxId();
// strTitle = num.ToString();
//}
}
else
{
strTitle = ddltitle.SelectedValue;
}
//当前日期 加/ 例子: 2009-6-25/
string strPath = DateTime.Now.ToShortDateString() + "/";
//文件名加后缀名
string fileName = picFile.PostedFile.FileName.Substring(picFile.PostedFile.FileName.LastIndexOf('//') + 1);
// 得到跟目录 /
string virtualPath = Page.Request.ApplicationPath;
//
string mPath = System.Web.HttpContext.Current.Request.MapPath("chuanmei/" + strTitle);
//得到绝对路径
string realPath = Server.MapPath("http://www.cnblogs.com/chuanmei/" + strTitle);
//判断是否存在,不存在就创建
if (!System.IO.Directory.Exists(realPath))
{
System.IO.Directory.CreateDirectory(realPath);
}
try
{
//字节大小
string size = picFile.PostedFile.ContentLength.ToString();
//获得后缀
string exName = picFile.PostedFile.FileName.Substring(picFile.PostedFile.FileName.LastIndexOf(".") + 1);
//文件名加后缀名
//string fn = System.IO.Path.GetFileName(picFile.PostedFile.FileName);
//string fn2 = "S-"+System.IO.Path.GetFileName(picFile.PostedFile.FileName);
string fn3 = System.IO.Path.GetFileName(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "." + exName);
string fn4 = "S-" + System.IO.Path.GetFileName(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "." + exName);
//保存文件
picFile.PostedFile.SaveAs(realPath + "//" + fn3);
//imguf.Src = "chuanmei/" + fn;
this.Label1.Text = "上传成功";
//文件相对路径
ViewState["picfile"] = "http://www.cnblogs.com/chuanmei/" + strTitle + "/" + fn3;
ViewState["S-picfile"] = "http://www.cnblogs.com/chuanmei/" + strTitle + "/" + fn4;
//文件绝对路径
ViewState["realpath"] = realPath;
ViewState["fileName"] = realPath + "//" + fn3;
ViewState["S-fileName"] = realPath + "//" + fn4;
ViewState["picsize"] = size;
ViewState["picexName"] = exName;
picUp.Enabled = true;
Label1.Visible = true;
dvLun.Style.Add("display", "block");
Image1.ImageUrl = "http://www.cnblogs.com/chuanmei/" + ViewState["picfile"].ToString();
//产生缩略图
MakeThumbnail(ViewState["fileName"].ToString(), ViewState["S-fileName"].ToString(), 50, 50, "HW");
return;
}
catch (Exception err)
{
Response.Write(err.ToString());
}
}
else
{
Label1.Text = "你没有选择要上载的文件或者上传的文件长度为0!";
}
}
2. //写Xml文件
XmlTextWriter writer = new XmlTextWriter(ViewState["realpath"].ToString() + "//content.xml", Encoding.UTF8); //( 绝对路径(包括文件名,格式 )
//缩进
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
//开始写Xml文档 <?xml version="1.0" encoding="utf-8"?>
writer.WriteStartDocument();
//写注释:<!—XXX->
writer.WriteComment("根节点");
//根节点content
writer.WriteStartElement("content");
//第一级子节点gallery
writer.WriteStartElement("gallery");
//一级子节点 属性
writer.WriteAttributeString("Name", "对应目录1");
writer.WriteAttributeString("Folder", "../" + ddltitle.SelectedValue);
for (int i = 0; i < ListCount; i++)
{
//二级子目录image
writer.WriteStartElement("image");
//属性
writer.WriteAttributeString("id", (i + 1).ToString());
writer.WriteAttributeString("Thumb", listCase[i].CASE_B2.Substring(listCase[i].CASE_B2.LastIndexOf('/') + 1));
writer.WriteAttributeString("Large", listCase[i].CASE_PICURL.Substring(listCase[i].CASE_PICURL.LastIndexOf('/') + 1));
writer.WriteAttributeString("Caption", listCase[i].CASE_CONTENT);
writer.WriteAttributeString("Colour", "414141");
//三级节点copy
writer.WriteStartElement("copy");
//节点的文本内容
//writer.WriteString("文本内容");
//写<[CDATA[..]]>
writer.WriteCData("<p><a href='#' target='_blank'></a></p>");
//copy
writer.WriteEndElement();
//image
writer.WriteEndElement();
}
//gallery
writer.WriteEndElement();
//content
writer.WriteEndElement();
//doc
writer.WriteEndDocument();
//将所有内容刷新到基础流
writer.Flush();
//关闭流和基础流
writer.Close();
#endregion
3. //文件拷贝
filecopy("swfobject.js");
filecopy("preview.swf");
filecopy("anli.html");
/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="filename">文件名</param>
private void filecopy(string filename)
{
string OrignFile, NewFile;
//源文件物理路径
OrignFile = Server.MapPath(filename);
//目标文件物理路径
NewFile = ViewState["realpath"] + "//" + filename;
FileInfo file = new FileInfo(OrignFile);
file.CopyTo(NewFile, true);
}