1、读取,添加,修改xml文件 /// <summary> /// 读取,添加,修改xml文件 /// </summary> /// <param name="Xmlpath">Xml路径</param> /// <param name="Node">新的子节点名称</param> /// <param name="Value">新节点对应的值</param> /// <param name="flag">1:读取,否则为 修改或者添加</param> /// <returns>1:修改添加成功,为空字符串表示修改添加成功,否则是读取成功</returns> public static string getXML(string Xmlpath, string Node, string Value, int flag) { try { string filepath = HttpContext.Current.Server.MapPath(Xmlpath); XmlDocument xmlDoc = new XmlDocument(); if (!File.Exists(filepath)) { XmlDeclaration xn = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); XmlElement root = xmlDoc.CreateElement("rss"); XmlElement root1 = xmlDoc.CreateElement("item"); root.AppendChild(root1); xmlDoc.AppendChild(xn); xmlDoc.AppendChild(root); xmlDoc.Save(filepath);//本地路径名字 } xmlDoc.Load(filepath);//你的xml文件 string ReStr = string.Empty; XmlElement xmlObj = xmlDoc.DocumentElement; XmlNodeList xmlList = xmlDoc.SelectSingleNode(xmlObj.Name.ToString()).ChildNodes; foreach (XmlNode xmlNo in xmlList) { if (xmlNo.NodeType != XmlNodeType.Comment)//判断是不是注释类型 { XmlElement xe = (XmlElement)xmlNo; { if (xe.Name == xmlObj.FirstChild.Name) { XmlNodeList xmlNList = xmlObj.FirstChild.ChildNodes; foreach (XmlNode xmld in xmlNList) { XmlElement xe1 = (XmlElement)xmld; { if (xe1.Name == Node) { if (flag == 1)//读取值 { if (xmld.InnerText != null && xmld.InnerText != "") { ReStr = xmld.InnerText; } } else//修改值 { xmld.InnerText = Value;//给节点赋值 xmlDoc.Save(filepath); ReStr = Value.Trim(); } } } } if (ReStr == string.Empty)// 添加节点 { XmlNode newNode; newNode = xmlDoc.CreateNode("element", Node, Value);//创建节点 newNode.InnerText = Value;//给节点赋值 xe.AppendChild(newNode);//把节点添加到doc xmlDoc.Save(filepath); ReStr = Value.Trim(); } } } } } return ReStr; } catch { return string.Empty; } } 2、取得文件扩展名 /// <summary> /// 取得文件扩展名 /// </summary> /// <param name="filename">文件名</param> /// <returns>扩展名</returns> public static string GetFileEXT(string filename) { if (string.IsNullOrEmpty(filename)) { return ""; } if (filename.IndexOf(@".") == -1) { return ""; } int pos = -1; if (!(filename.IndexOf(@"/") == -1)) { pos = filename.LastIndexOf(@"/"); } string[] s = filename.Substring(pos + 1).Split('.'); return s[1]; } 3、替换文本中的空格和换行 /// <summary> /// 替换文本中的空格和换行 /// </summary> public static string ReplaceSpace(string str) { string s = str; s = s.Replace(" ", " "); s = s.Replace("/n", "<BR />"); return s; } 4、验证码实现方法 protected void Page_Load(object sender, EventArgs e) { string checkCode = CreateRandomCode(4); Session["CheckCode"] = checkCode; CreateImage(checkCode); } private string CreateRandomCode(int codeCount) { // 函数功能:产生数字和字符混合的随机字符串 string allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[] allCharArray = allChar.ToCharArray(); string randomCode = ""; Random rand = new Random(); for (int i = 0; i < codeCount; i++) { int r=rand.Next(61); randomCode+=allCharArray.GetValue(r); } return randomCode; } private void CreateImage(string checkCode) { // 生成图象验证码函数 int iwidth = (int)(checkCode.Length * 11.5); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 20); Graphics g = Graphics.FromImage(image); Font f = new System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(Color.Azure);//字母白色 //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height); g.Clear(Color.Brown);//背景灰色 g.DrawString(checkCode, f, b, 3, 3); Pen blackPen = new Pen(Color.Black, 0); Random rand = new Random(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); Response.ClearContent(); Response.ContentType = "image/Jpeg"; Response.BinaryWrite(ms.ToArray()); g.Dispose(); image.Dispose(); } 5、文件创建、复制、移动、删除 FileStream fs; //创建文件 fs = File.Create(Server.MapPath("a.txt")); fs.Close(); fs = File.Create(Server.MapPath("b.txt")); fs.Close(); fs = File.Create(Server.MapPath("c.txt")); fs.Close(); //复制文件 File.Copy(Server.MapPath("a.txt"), Server.MapPath("a//a.txt")); //移动文件 File.Move(Server.MapPath("b.txt"), Server.MapPath("a//b.txt")); File.Move(Server.MapPath("c.txt"), Server.MapPath("a//c.txt")); //删除文件 File.Delete(Server.MapPath("a.txt")); 6、生成任意位数的随机数 /// <summary> /// 生成随机数 /// </summary> /// <param name="minValue">最小值</param> /// <param name="maxValue">最大值</param> /// <returns></returns> private int getRandom(int minValue, int maxValue) { Random ri = new Random(unchecked((int)DateTime.Now.Ticks)); int k = ri.Next(minValue, maxValue); return k; } //想定一个三位的随机数:string ThreeRandom=this.getRandom(100,999).Tostring(); //类似的,四位随机数:string FourRandom=this.getRandom(1000,9999).Tostring();