简单的文件操作方法和xml操作方法

显示xml数据
 1     /// <summary>
 2     /// 文件列表
 3     /// </summary>
 4     /// <param name="FileName">文件名</param>
 5     /// <param name="FileType">文件类型</param>
 6     /// <param name="FileSize">文件大小</param>
 7     /// <param name="UpLoadTime">上传时间</param>
 8     /// <param name="DownCount">下载次数</param>
 9     /// <param name="context"></param>
10     /// <returns>返回结果</returns>
11         private bool FileList(string FileName, string FileType, string FileSize, string UpLoadTime, int DownCount,string Path,string NPath, HttpContext context)
12         {
13             bool addXmlDate = false;  //返回是不是要添加数据
14 
15             XmlDocument xmlDoc = new XmlDocument();
16 
17             xmlDoc.Load(context.Server.MapPath("http://www.cnblogs.com/source/list.xml")); //把XML文件装载进来
18 
19             //定义根节点Files
20             XmlNode root = xmlDoc.SelectSingleNode("Files");
21 
22             XmlNodeList xnl = root.ChildNodes;
23 
24             //节点File
25             XmlElement xel = xmlDoc.CreateElement("File");
26 
27             if (xnl.Count > 0)
28             {
29                 xel.SetAttribute("id", (xnl.Count).ToString());
30             }
31             else
32             {
33                 xel.SetAttribute("id", (0).ToString());
34             }
35 
36             XmlElement username = xmlDoc.CreateElement("FileName");
37 
38             username.InnerText = FileName;
39 
40             xel.AppendChild(username);
41 
42             //文件类型
43             XmlElement xesubTitle = xmlDoc.CreateElement("FileType");
44 
45             xesubTitle.InnerText = FileType;
46 
47             xel.AppendChild(xesubTitle);
48 
49             //文件大小
50 
51             XmlElement xesubAuthor = xmlDoc.CreateElement("FileSize");
52 
53             xesubAuthor.InnerText = FileSize;
54 
55             xel.AppendChild(xesubAuthor);
56 
57             //上传时间
58             XmlElement xesubPrice = xmlDoc.CreateElement("UpLoadTime");
59 
60             xesubPrice.InnerText = UpLoadTime;
61 
62             xel.AppendChild(xesubPrice);
63 
64             //下载次数
65             XmlElement cs = xmlDoc.CreateElement("DownCount");
66 
67             cs.InnerText = DownCount.ToString();
68 
69             xel.AppendChild(cs);
70 
71             //存放根路径
72             XmlElement p = xmlDoc.CreateElement("Path");
73 
74             p.InnerText = Path;
75 
76             xel.AppendChild(p);
77 
78             //存放相对路径
79             XmlElement n = xmlDoc.CreateElement("NPath");
80 
81             n.InnerText = NPath;
82 
83             xel.AppendChild(n);
84 
85             //添加到 Files的root里去
86 
87             root.AppendChild(xel);
88 
89             xmlDoc.Save(context.Server.MapPath("http://www.cnblogs.com/source/list.xml"));    //保存添加的数据
90 
91             addXmlDate = true;
92 
93             return addXmlDate;
94 
95         }

 

上传文件
 1         /// <summary>
 2         /// 文件上传方法
 3         /// </summary>
 4         /// <param name="filepath">文件原始路径</param>
 5         /// <param name="savepath">文件保存路径</param>
 6         /// <param name="upload">是否上传</param>
 7         /// <param name="context">http请求实例</param>
 8         /// <returns>返回上传结果</returns>
 9         public bool UpLoad(string savepath,bool upload,HttpContext context,HttpPostedFile post)
10         {
11             //string pa=Path.GetExtension(post.FileName);
12             //FileInfo FIleIn = new FileInfo(context.Server.MapPath(pa));
13             string name = Path.GetFileName(post.FileName);
14             string path = null;
15             //string[] array = name.Split('.');
16 
17             //判断保存目录是否存在,若不存在则创建该目录
18             if (!System.IO.Directory.Exists(savepath))
19             {
20                 System.IO.Directory.CreateDirectory(context.Server.MapPath(ConfigHelper.SitePath +savepath));
21             }
22             //FIleIn = new FileInfo(context.Server.MapPath(FIleIn.DirectoryName)); //文件信息
23 
24             if (string.IsNullOrEmpty(savepath))
25             {
26                 path = ConfigHelper.SitePath + "AuthPage/source/upload/";
27                 savepath = context.Server.MapPath(path);
28             }
29             else
30             {
31                 path = ConfigHelper.SitePath + savepath + "/";
32                 savepath = context.Server.MapPath(path);
33             }
34                 //string filename = filepath.FileName;
35       
36       
37                 //if (FIleIn.Exists)
38                 //{
39                     //获取文件大小
40                     string size = ConvertUnit(post.ContentLength);
41 
42                     //获取文件类型
43                     string type = Path.GetExtension(name);
44                     string typestr = type.Substring(1);
45 
46 
47                     if (!System.IO.File.Exists(savepath + name))
48                     {
49                         post.SaveAs(savepath + name);
50                         FileList(name, typestr, size, DateTime.Now.ToString(), 0, savepath+name,path + name, context);
51                         context.Response.Write("0");
52                     }
53                     else
54                     {
55                         post.SaveAs(savepath + name);
56                         context.Response.Write("1");
57                     }
58 
59             return true;
60         }

 

删除xml数据
 1         /// <summary>
 2         /// 删除xml数据
 3         /// </summary>
 4         /// <param name="delstr">参数id</param>
 5         /// <param name="filepath1">文件路径</param>
 6         /// <param name="context">实例</param>
 7         /// <returns>返回结果</returns>
 8         public bool DelXML( HttpContext context, HttpPostedFile post)
 9         {
10             string str = context.Request.Params["id"];
11             XmlDocument xmlDoc = new XmlDocument();
12             xmlDoc.Load(context.Server.MapPath("http://www.cnblogs.com/source/list.xml"));
13             XmlNode root = xmlDoc.SelectSingleNode("Files");
14             XmlNodeList xnl = root.ChildNodes;
15             for (int i = 0; i < xnl.Count; i++)
16             {
17                 XmlElement xe = (XmlElement)xnl.Item(i);
18                 //XmlNode xn = (XmlNode)xnl.Item(i);
19                 if (xe.GetAttribute("id") == str)
20                 {
21                     
22                     DelFile(xe.ChildNodes[5].InnerText, true, context);
23                     root.RemoveChild(xe);
24                     
25                 }
26             }
27            
28 
29            
30             xmlDoc.Save(context.Server.MapPath("http://www.cnblogs.com/source/list.xml"));
31             HttpResponse Respone = context.Response;
32             Respone.Redirect("http://www.cnblogs.com/source/list.aspx");
33             return true;
34         }

 

统计下载次数
 1 文件删除 
 2  1         /// <summary>
 3  2         /// 删除文件方法
 4  3         /// </summary>
 5  4         /// <param name="filepath"></param>
 6  5         /// <param name="del"></param>
 7  6         /// <param name="context"></param>
 8  7         /// <param name="post"></param>
 9  8         /// <returns></returns>
10  9         public bool DelFile(string delpath, bool del, HttpContext context)
11 10         {
12 11           
13 12             if (!System.IO.File.Exists(delpath))
14 13             {
15 14                 context.Response.Write("0");
16 15             }
17 16             else
18 17             {
19 18                 System.IO.File.Delete(delpath);
20 19                 context.Response.Write("1");
21 20             }
22 21             return true;
23 22         }
24 23 
25 24    
26 25 
27 26     /// <summary>
28 27     /// 下载次数
29 28     /// </summary>
30 29     /// <param name="context">实例化对象</param>
31 30         /// <param name="post">实例化对象</param>
32 31     /// <returns>返回修改结果</returns>
33 32         public bool Update(HttpContext context, HttpPostedFile post)
34 33         {
35 34             //string d = context.Request.Params["p"];
36 35             string str = context.Request.Params["id"];
37 36             XmlDocument xmlDoc = new XmlDocument();
38 37             xmlDoc.Load(context.Server.MapPath("http://www.cnblogs.com/source/list.xml"));
39 38             XmlNode root = xmlDoc.SelectSingleNode("Files");
40 39             XmlNodeList xnl = root.ChildNodes;
41 40             for (int i = 0; i < xnl.Count; i++)
42 41             {
43 42                 XmlElement xe = (XmlElement)xnl.Item(i);
44 43                 //XmlNode xn = (XmlNode)xnl.Item(i);
45 44                 if (xe.GetAttribute("id") == str)
46 45                 {
47 46                     int down = int.Parse(xe.ChildNodes[4].InnerText);
48 47                     down = down + 1;
49 48                 }
50 49             }
51 50 
52 51             xmlDoc.Save(context.Server.MapPath("http://www.cnblogs.com/source/list.xml"));
53 52             return true;
54 53         }

 

单位转换
 1 View Code 
 2  1         /// <summary>
 3  2         /// 单位转换
 4  3         /// </summary>
 5  4         /// <param name="size">byte</param>
 6  5         /// <returns></returns>
 7  6         public static string ConvertUnit(long size)
 8  7         {
 9  8             string FileSize = string.Empty;
10  9             if (size > (1024 * 1024 * 1024))
11 10                 FileSize = ((double)size / (1024 * 1024 * 1024)).ToString(".##") + " GB";
12 11             else if (size > (1024 * 1024))
13 12                 FileSize = ((double)size / (1024 * 1024)).ToString(".##") + " MB";
14 13             else if (size > 1024)
15 14                 FileSize = ((double)size / 1024).ToString(".##") + " KB";
16 15             else if (size == 0)
17 16                 FileSize = "0 Byte";
18 17             else
19 18                 FileSize = ((double)size / 1).ToString(".##") + " Byte";
20 19 
21 20             return FileSize;
22 21         }

 

转载于:https://www.cnblogs.com/AlphaThink-AT003/archive/2013/02/03/XML.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值