c#合适新手练习的Demo案例

1:操作功能
注册
登录
退出
2:功能实现
    2.1:使用二级关联菜单选择城市
    2.2:上传图片,使用相对路径灵活保存。
    2.3:在登录成功后回显用户数据,并提供下载图片

    2.4:使用xml保存数据。

3:核心代码

|--xml保存数据库

/*
 * 由SharpDevelop创建。
 * 用户: Administrator
 * 日期: 03/13/2014
 * 时间: 21:41
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 * 
 */
using System;
using System.Xml;
using System.Collections;
using demo_chechBox.baseMain;
using System.Text;

namespace demo_chechBox.tool
{
	/*解析城市xml*/
	public class AnalyticalXml
	{
		public static string  citiesXmlName = "../../xml/comboBox/Cities.xml";
		public static string districtsXmlName = "../../xml/comboBox/Districts.xml";
		public static string userXmlName = "../../xml/dataSource/user.xml";
		
		public AnalyticalXml()
		{
		}
		
		/*获取所有城市*/
		public static IList findAllCity()
		{
			IList array = new ArrayList();
			
			XmlDocument xmlDoc = getXmlDocument(citiesXmlName);
			
			XmlNodeList xnlList = xmlDoc.SelectNodes("Cities/City");
			
			foreach(XmlNode x in xnlList)
			{
				array.Add(x.InnerText);
			}
			
			return array;
		}
		
		/**根据城市查询区*/
		public static IList findAllDistricts(string city)
		{
			IList array = new ArrayList();
			
			//获取城市ID
			XmlElement xe = getElementByCity(city);
			string cityId = xe.GetAttribute("ID");
			
			//查询城市下边的子区
			XmlDocument xmldoc = getXmlDocument(districtsXmlName);
			XmlNodeList nodeList = xmldoc.SelectNodes("Districts/District[@CID='"+cityId+"']");
			
			foreach (XmlNode xn in nodeList) {
				array.Add(xn.InnerText);
			}
			
			return array;
		}
		
		/**以username为条件查询指定User*/
		public static User getUser(string username)
		{
			XmlDocument xmldoc = getXmlDocument(userXmlName);
			
			XmlElement xe = (XmlElement)xmldoc.SelectNodes("users/user[@username='"+username+"']")[0];
			if(xe!=null)
			{
				User user = new User();
				user.setPassword(xe.GetAttribute("password"));
				user.setUsername(xe.GetAttribute("username"));
				user.setAddress(xe.GetAttribute("address"));
				user.setFileName(xe.GetAttribute("fileName"));
				user.setHobby(xe.GetAttribute("hobby"));
				user.setPhoto(xe.GetAttribute("photo"));
				user.setRemarks(xe.GetAttribute("remarks"));
				user.setBirtday(xe.GetAttribute("birtday"));
				user.setGender(xe.GetAttribute("gender"));
				return user;
			}
			return null;
		}
			
		
		public static XmlElement getElementByCity(string city)
		{
			
			XmlDocument xmlDoc = getXmlDocument(citiesXmlName);
			
			XmlNode xmlde = xmlDoc.SelectNodes("Cities/City[@CityName='"+city+"']")[0];
			
			XmlElement xe = xmlde as XmlElement;
			
			return xe;
		}
		
		//保存user
		public static void saveUser(User user)
		{
			XmlDocument xmldoc = getXmlDocument(userXmlName);
			XmlNode root = xmldoc.DocumentElement;
			
			//创建新节点
			XmlElement newChild = xmldoc.CreateElement("user");
			newChild.SetAttribute("guid",user.Guid);
			newChild.SetAttribute("username",user.getUsername());
			newChild.SetAttribute("password",user.getPassword());
			newChild.SetAttribute("gender",user.getGender());
			newChild.SetAttribute("birtday",user.getBirtday());
			newChild.SetAttribute("hobby",user.getHobby());
			newChild.SetAttribute("address",user.getAddress());
			newChild.SetAttribute("photo",user.getPhoto());
			newChild.SetAttribute("remarks",user.getRemarks());
			newChild.SetAttribute("fileName",user.getFileName());
			
			//添加users的最后一个子级节点后面
			//XmlNode refChild = xmldoc.SelectNodes("users")[0];
			XmlNode child = root.LastChild;
			if(child!=null)
			{
				root.InsertAfter(newChild,root.LastChild);
			}
			else
			{
				root.AppendChild(newChild);
			}
			
			//保存
			XmlTextWriter xmlWriter = new XmlTextWriter(userXmlName,Encoding.GetEncoding("utf-8"));
			xmlWriter.Formatting = Formatting.Indented;
			xmldoc.Save(xmlWriter);
			xmlWriter.Close();
		}
		
		
		public static XmlDocument getXmlDocument(string xmlName)
		{
			XmlDocument xmldoc = new XmlDocument();
			try
			{
				xmldoc.Load(xmlName);
			}
			catch(Exception ex)
			{
				throw new OverflowException("找不到"+xmlName+"文件,抛出"+ex.Message);
			}
			return xmldoc;
		}
	}
}


|--结合控件完成,灵活完成上传下载

上传

		public void SaveImgFile(User user)
		{
			string photo = user.getPhoto();
			//保存文件名
			user.setFileName(Path.GetFileName(photo));
			
			//保存的路径
			DateTime dt = DateTime.Now;
			string guidName = Guid.NewGuid().ToString().Replace("-","");
			string extensionName = Path.GetExtension(photo);
		
			string createDirectory = string.Format("upload\\{0}",dt.ToString("yyyy-MM-dd"));
			if(!Directory.Exists(createDirectory))
			{	
				Directory.CreateDirectory(createDirectory);
			}
			user.setPhoto(string.Format("{0}\\{1}{2}",createDirectory,guidName,extensionName));
				
			string savePath = System.Environment.CurrentDirectory;
			savePath = string.Format("{0}\\{1}",savePath,user.getPhoto());
			
			File.Copy(photo,savePath);
		}


下载

	
void DwonloadClick(object sender, EventArgs e)
		{
			saveFile.FileName = "c:\\"+label_fileName.Text;
			saveFile.Filter = "jpg文件(*.jpg)|*.jpg|gif文件(*.gif)|*.gif|png文件(*.png)|*.png";
			saveFile.InitialDirectory = @"D:\Documents\Pictures";
			 if(saveFile.ShowDialog()==DialogResult.OK)
  			  {
			      string filename=saveFile.FileName;
			      SaveFile(filename);
			  }
				//Stream stream = saveFile.OpenFile();
		}
		public void SaveFile(string fileName)
		{
		  Image image = showUploadImg.Image;
	      Bitmap bit = new Bitmap(image);
	      bit.Save(fileName,  
	      System.Drawing.Imaging.ImageFormat.Bmp);
		}
	}
 
  
 
  
 
  

下面介绍功能演示图

c#合适新手练习的Demo案例_第1张图片c#合适新手练习的Demo案例_第2张图片c#合适新手练习的Demo案例_第3张图片

项目源代码下载

你可能感兴趣的:(c#)